import { TaskbarElement } from "../../../snk-taskbar/elements/taskbar-elements";
import { ApplicationContext } from "@sankhyalabs/core";
let messagesBuilder;
const getMessage = (key) => {
  var _a;
  if (!messagesBuilder) {
    const application = ApplicationContext.getContextValue("__SNK__APPLICATION__");
    messagesBuilder = application === null || application === void 0 ? void 0 : application.messagesBuilder;
  }
  return (_a = messagesBuilder === null || messagesBuilder === void 0 ? void 0 : messagesBuilder.getMessage) === null || _a === void 0 ? void 0 : _a.call(messagesBuilder, key, null);
};
export const TaskbarAttachButtons = {
  DOWNLOAD: {
    hint: getMessage("snkAttach.taskbar.titleDownload"),
    name: "DOWNLOAD",
    iconName: 'file-download'
  },
  LINK: {
    hint: getMessage("snkAttach.taskbar.titleLink"),
    name: "LINK",
    iconName: "launch"
  }
};
export const buildTaskBarManager = () => {
  const disabledButtonsWithoutSelection = [
    TaskbarElement.REMOVE,
    "DOWNLOAD",
    "LINK"
  ];
  return {
    getButtons: (_, dataState, currentButtons) => {
      const refreshBtnIndex = currentButtons.indexOf(TaskbarElement.REFRESH);
      if ((dataState === null || dataState === void 0 ? void 0 : dataState.insertionMode) || (dataState === null || dataState === void 0 ? void 0 : dataState.isDirty)) {
        return currentButtons.reverse();
      }
      else if (refreshBtnIndex !== -1) {
        currentButtons.splice(refreshBtnIndex, 1);
      }
      const { selectedRecord } = dataState || {};
      const taskbarTobeAdded = !!(selectedRecord === null || selectedRecord === void 0 ? void 0 : selectedRecord.LINK) ? TaskbarAttachButtons.LINK : TaskbarAttachButtons.DOWNLOAD;
      currentButtons.splice(currentButtons.indexOf(TaskbarElement.DIVIDER) + 1, 0, TaskbarElement.REMOVE, taskbarTobeAdded, TaskbarElement.DIVIDER);
      const buttonsToRemove = [TaskbarElement.CLONE, TaskbarElement.DATA_EXPORTER];
      const updatedButtons = Array.from(new Set(currentButtons.filter((button) => !buttonsToRemove.includes(button))));
      updatedButtons.splice(updatedButtons.indexOf(taskbarTobeAdded) + 1, 0, TaskbarElement.DIVIDER);
      return updatedButtons;
    },
    isEnabled: (_, dataState, currentButton) => {
      const hasSelectedRecord = (dataState === null || dataState === void 0 ? void 0 : dataState.selectedRecord) !== undefined;
      const disabledButton = disabledButtonsWithoutSelection.includes(currentButton);
      if (disabledButton && !hasSelectedRecord)
        return false;
      return true;
    }
  };
};
