<!--

    Copyright © 2016-2026 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div class="tb-js-func flex flex-col" style="background: #fff;" [class]="{'tb-disabled': disabled, 'fill-height': fillHeight, 'tb-hide-brackets': hideBrackets}"
     tb-fullscreen
     [fullscreen]="fullscreen">
  <div style="min-height: 40px;" class="tb-js-func-toolbar flex flex-row items-center justify-start">
    @if (!hideLabel) {
      <label class="tb-title no-padding"
             [class]="{'tb-error': !disabled && (hasErrors || !functionValid || required && !modelValue), 'tb-required': !disabled && required}">
        {{ functionLabel }}
      </label>
    }
    <ng-content select="[toolbarStartButton]"></ng-content>
    <span class="flex-1"></span>
    <ng-content select="[toolbarPrefixButton]"></ng-content>
    @if (!disabled && withModules) {
      <fieldset style="width: initial">
        <div matTooltip="{{'js-func.modules' | translate}}"
             matTooltipPosition="above"
             style="border-radius: 50%"
             #editModulesButton
             (click)="editModules($event, editModulesButton)">
          <button type='button' mat-icon-button class="tb-mat-32">
            <tb-icon color="primary" matButtonIcon>mdi:application-braces-outline</tb-icon>
          </button>
        </div>
      </fieldset>
    }
    @if (!disabled) {
      <button type='button' mat-button class="tidy" (click)="beautifyJs()">
        {{'js-func.tidy' | translate }}
      </button>
    }
    <ng-content select="[toolbarSuffixButton]"></ng-content>
    @if (helpId) {
      <div [tb-help-popup]="helpId" [tb-help-popup-style]="helpPopupStyle"></div>
    }
    <fieldset style="width: initial">
      <div matTooltip="{{(fullscreen ? 'fullscreen.exit' : 'fullscreen.expand') | translate}}"
           matTooltipPosition="above"
           style="border-radius: 50%"
           (click)="fullscreen = !fullscreen">
        <button type='button' mat-icon-button class="tb-mat-32">
          <mat-icon class="material-icons">{{ fullscreen ? 'fullscreen_exit' : 'fullscreen' }}</mat-icon>
        </button>
      </div>
    </fieldset>
  </div>
  <div id="tb-javascript-panel" class="tb-js-func-panel flex flex-col" tb-toast toastTarget="{{toastTargetId}}">
    <div #javascriptEditor id="tb-javascript-input" [style]="fillHeight ? {} : {minHeight: minHeight}" [class.fill-height]="fillHeight"></div>
  </div>
  @if (!hideBrackets) {
    <div class="flex flex-row items-center justify-start" style="height: 40px;">
      <label class="tb-title no-padding" [class]="{'tb-error': hasErrors || !functionValid || required && !modelValue}">&#125;</label>
    </div>
  }
</div>
