import './DataFetcher-5ed0a377.js';
import './ISave-5b46d691.js';
import '@sankhyalabs/core';
import { P as PresentationMode } from './index-b40568ff.js';
import '@sankhyalabs/ezui/dist/collection/utils/constants';
import '@sankhyalabs/core/dist/dataunit/metadata/UnitMetadata';
import './dataunit-fetcher-3b1ca96e.js';
import './filter-item-type.enum-d45e026f.js';
import './form-config-fetcher-1c81efc4.js';
import { V as VIEW_MODE } from './constants-7b422de0.js';
import { T as TaskbarElement } from './taskbar-elements-63651ca7.js';

class SimpleTaskbarProcessor {
  /**
   * Cria uma nova instância do processador de taskbar.
   * @param defaultButtons - Array com os botões padrão a serem exibidos
  */
  constructor(defaultButtons) {
    this._defaultButtons = [];
    this._defaultButtons = [...defaultButtons];
  }
  /**
   * Processa a taskbar aplicando regras de visibilidade e habilitação.
   * @param taskbarID - Identificador único da taskbar
   * @param taskbarManager - Gerenciador de taskbar com regras customizadas
   * @param dataState - Estado atual dos dados
   * @param disabledButtons - Array de botões desabilitados
   * @param invisibleButtons - Array de botões invisíveis
 */
  process(taskbarID, taskbarManager, dataState, disabledButtons = [], invisibleButtons = []) {
    var _a;
    this.taskbarID = taskbarID;
    this.customButtons = new Map();
    this.buttonsArray = [];
    this.disabledButtons = [...disabledButtons];
    (_a = this.getButtonsArray(taskbarManager, dataState)) === null || _a === void 0 ? void 0 : _a.forEach((btnDef) => {
      let buttonName;
      if (typeof btnDef === "string") {
        buttonName = btnDef;
      }
      else {
        this.customButtons.set(btnDef.name, btnDef);
        buttonName = btnDef.name;
      }
      if (!invisibleButtons.includes(buttonName)) {
        if (!this.isEnabled(taskbarManager, buttonName, dataState, disabledButtons)) {
          this.disabledButtons.push(buttonName);
        }
        this.buttonsArray.push(buttonName);
      }
    });
    this.buttons = this.buttonsArray.join(",");
  }
  getButtonsArray(taskbarManager, dataState) {
    if (taskbarManager) {
      return taskbarManager.getButtons(this.taskbarID, dataState, [...this._defaultButtons]);
    }
    return this._defaultButtons;
  }
  isEnabled(taskbarManager, buttonName, dataState, disabled) {
    const originalValue = !disabled.includes(buttonName);
    if (taskbarManager && taskbarManager.isEnabled) {
      return taskbarManager.isEnabled(this.taskbarID, dataState, buttonName, originalValue);
    }
    return originalValue;
  }
}

const DEFAULT_TASKBAR_BUTTONS = [
  TaskbarElement.FORM_MODE,
  TaskbarElement.PREVIOUS,
  TaskbarElement.NEXT,
  TaskbarElement.REFRESH,
  TaskbarElement.SPACER,
  TaskbarElement.ACTIONS_BUTTON,
  TaskbarElement.DATA_EXPORTER,
  TaskbarElement.ATTACH,
  TaskbarElement.CLONE,
  TaskbarElement.UPDATE,
  TaskbarElement.REMOVE,
  TaskbarElement.INSERT,
  TaskbarElement.CONFIGURATOR,
  TaskbarElement.MORE_OPTIONS,
];
var CustomTaskbarButton;
(function (CustomTaskbarButton) {
  CustomTaskbarButton["FILTER"] = "FILTER";
  CustomTaskbarButton["ENTITY_SEARCH"] = "ENTITY_SEARCH";
})(CustomTaskbarButton || (CustomTaskbarButton = {}));
/**
 * Insere os botões customizados à esquerda do elemento SPACER na lista de botões.
 * Caso o SPACER não seja encontrado, os botões são inseridos no início da lista.
 */
function insertCustomButtonsBeforeSpacer(buttons, customButtons) {
  const spacerIndex = buttons.findIndex(btn => btn === TaskbarElement.SPACER || (typeof btn === 'string' && btn === 'SPACER'));
  if (spacerIndex > -1) {
    return [
      ...buttons.slice(0, spacerIndex),
      ...customButtons,
      ...buttons.slice(spacerIndex),
    ];
  }
  return [...customButtons, ...buttons];
}
function buildNavigationDisabledBtns(dataState) {
  const navigationDisabled = [];
  if (!(dataState === null || dataState === void 0 ? void 0 : dataState.hasPrevious)) {
    navigationDisabled.push(TaskbarElement.PREVIOUS);
  }
  if (!(dataState === null || dataState === void 0 ? void 0 : dataState.hasNext)) {
    navigationDisabled.push(TaskbarElement.NEXT);
  }
  return navigationDisabled;
}
function buildNativeTaskbarManager(currentViewMode, mergeTaskbarManager, messagesBuilder) {
  var _a, _b;
  const CUSTOM_BUTTONS = [
    {
      name: CustomTaskbarButton.FILTER,
      hint: (_a = messagesBuilder === null || messagesBuilder === void 0 ? void 0 : messagesBuilder.getMessage("snkCrud.customButtons.filter")) !== null && _a !== void 0 ? _a : "Abre o painel de filtros",
      iconName: "filter",
    },
    {
      name: CustomTaskbarButton.ENTITY_SEARCH,
      hint: (_b = messagesBuilder === null || messagesBuilder === void 0 ? void 0 : messagesBuilder.getMessage("snkCrud.customButtons.entitySearch")) !== null && _b !== void 0 ? _b : "Busca na entidade",
      iconName: "search",
    },
  ];
  const EDITION_MODE_BUTTONS = [
    TaskbarElement.CANCEL,
    TaskbarElement.SAVE,
  ];
  return {
    getButtons: (taskbarID, dataState, defaultButtons) => {
      var _a, _b;
      if (!taskbarID) {
        console.warn('taskbarID não fornecido');
        return [];
      }
      const isInEditionMode = (dataState === null || dataState === void 0 ? void 0 : dataState.insertionMode) || (dataState === null || dataState === void 0 ? void 0 : dataState.isDirty);
      if (isInEditionMode) {
        return EDITION_MODE_BUTTONS;
      }
      if (currentViewMode === VIEW_MODE.FORM) {
        const indexFormMode = defaultButtons.indexOf(TaskbarElement.FORM_MODE);
        if (indexFormMode > -1) {
          defaultButtons = [
            ...defaultButtons.slice(0, indexFormMode),
            TaskbarElement.GRID_MODE,
            ...defaultButtons.slice(indexFormMode + 1)
          ];
        }
      }
      if (((_b = (_a = dataState === null || dataState === void 0 ? void 0 : dataState.selectionInfo) === null || _a === void 0 ? void 0 : _a.records) === null || _b === void 0 ? void 0 : _b.length) > 1) {
        const indexUpdate = defaultButtons.indexOf(TaskbarElement.UPDATE);
        if (indexUpdate > -1) {
          defaultButtons = [
            ...defaultButtons.slice(0, indexUpdate),
            TaskbarElement.UPDATE_MULTIPLE,
            ...defaultButtons.slice(indexUpdate + 1)
          ];
        }
      }
      const defaultsWithCustomButtons = insertCustomButtonsBeforeSpacer(defaultButtons, CUSTOM_BUTTONS);
      if (mergeTaskbarManager === null || mergeTaskbarManager === void 0 ? void 0 : mergeTaskbarManager.getButtons) {
        return mergeTaskbarManager.getButtons(taskbarID, dataState, defaultsWithCustomButtons);
      }
      return defaultsWithCustomButtons;
    },
    isEnabled: (_, dataState, buttonName, originalValue) => {
      var _a;
      if (!dataState) {
        return originalValue;
      }
      const hasSelectedRecord = dataState.selectedRecord !== undefined;
      const buttonsRequiringSelection = [
        TaskbarElement.UPDATE,
        TaskbarElement.REMOVE,
        TaskbarElement.ATTACH,
        TaskbarElement.CLONE,
      ];
      if (!hasSelectedRecord && buttonsRequiringSelection.includes(buttonName)) {
        return false;
      }
      const navigationDisabled = buildNavigationDisabledBtns(dataState);
      if (navigationDisabled.includes(buttonName)) {
        return false;
      }
      return (_a = mergeTaskbarManager === null || mergeTaskbarManager === void 0 ? void 0 : mergeTaskbarManager.isEnabled(_, dataState, buttonName, originalValue)) !== null && _a !== void 0 ? _a : originalValue;
    },
    getMoreOptions(taskbarId, configName, dataState, currentActions) {
      var _a;
      return (_a = mergeTaskbarManager === null || mergeTaskbarManager === void 0 ? void 0 : mergeTaskbarManager.getMoreOptions) === null || _a === void 0 ? void 0 : _a.call(mergeTaskbarManager, taskbarId, configName, dataState, currentActions);
    },
  };
}
function getHeaderTaskbarId(dataState, presentationMode) {
  var _a;
  const hasSelectedRecord = dataState && !!((_a = dataState.selectionInfo) === null || _a === void 0 ? void 0 : _a.length);
  const taskbarIdPrefixLookup = {
    primary: "snkGridHeaderTaskbar",
    secondary: "snkGridHeaderTaskbar",
    singleTaskbar: "snkGridHeaderTaskbar.singleTaskbar",
  };
  const taskbarIdPrefix = taskbarIdPrefixLookup[presentationMode];
  let headerTaskbarId = hasSelectedRecord ? `${taskbarIdPrefix}.selected` : `${taskbarIdPrefix}.unselected`;
  if ((dataState === null || dataState === void 0 ? void 0 : dataState.isDirty) && presentationMode === PresentationMode.SINGLE_TASKBAR) {
    headerTaskbarId = "snkGridHeaderTaskbar.singleTaskbar.finish_edition";
  }
  return headerTaskbarId;
}
function getTopTaskBarId(dataState, presentationMode) {
  const suffixLookup = {
    primary: '',
    secondary: '.secondary',
    singleTaskbar: '.singleTaskbar',
  };
  const sufix = suffixLookup[presentationMode];
  if (dataState === null || dataState === void 0 ? void 0 : dataState.isDirty) {
    return `snkGridTopTaskbar.finish_edition${sufix}`;
  }
  return `snkGridTopTaskbar.regular${sufix}`;
}

export { CustomTaskbarButton as C, DEFAULT_TASKBAR_BUTTONS as D, SimpleTaskbarProcessor as S, getHeaderTaskbarId as a, buildNativeTaskbarManager as b, getTopTaskBarId as g };
