import { h } from "@stencil/core";
import { PresentationMode } from "../../../lib/@types";
import store from "../../../lib/store";
export var TaskbarElement;
(function (TaskbarElement) {
  TaskbarElement["PREVIOUS"] = "PREVIOUS";
  TaskbarElement["NEXT"] = "NEXT";
  TaskbarElement["REFRESH"] = "REFRESH";
  TaskbarElement["UPDATE"] = "UPDATE";
  TaskbarElement["CLONE"] = "CLONE";
  TaskbarElement["REMOVE"] = "REMOVE";
  TaskbarElement["INSERT"] = "INSERT";
  TaskbarElement["CANCEL"] = "CANCEL";
  TaskbarElement["SAVE"] = "SAVE";
  TaskbarElement["GRID_MODE"] = "GRID_MODE";
  TaskbarElement["FORM_MODE"] = "FORM_MODE";
  TaskbarElement["MORE_OPTIONS"] = "MORE_OPTIONS";
  TaskbarElement["DIVIDER"] = "DIVIDER";
  TaskbarElement["SPACER"] = "SPACER";
  TaskbarElement["CONFIGURATOR"] = "CONFIGURATOR";
  TaskbarElement["DATA_EXPORTER"] = "DATA_EXPORTER";
  TaskbarElement["ATTACH"] = "ATTACH";
  TaskbarElement["ACTIONS_BUTTON"] = "ACTIONS_BUTTON";
  TaskbarElement["UPDATE_MULTIPLE"] = "UPDATE_MULTIPLE";
})(TaskbarElement || (TaskbarElement = {}));
export var AuthorizationElements;
(function (AuthorizationElements) {
  AuthorizationElements["UPDATE"] = "UPDATE";
  AuthorizationElements["CLONE"] = "CLONE";
  AuthorizationElements["REMOVE"] = "REMOVE";
  AuthorizationElements["INSERT"] = "INSERT";
  AuthorizationElements["CONFIGURATOR"] = "CONFIGURATOR";
})(AuthorizationElements || (AuthorizationElements = {}));
export var VisibleWhenForbidden;
(function (VisibleWhenForbidden) {
  VisibleWhenForbidden["CONFIGURATOR"] = "CONFIGURATOR";
})(VisibleWhenForbidden || (VisibleWhenForbidden = {}));
export const buildCustomButton = ({ def, className, dataElementId, action, isEnabled, size }) => {
  const { hint, text, iconName } = def;
  if (iconName) {
    if (text) {
      return iconTextButton({ iconName, element: def.name, className, dataElementId, title: hint, action, isEnabled, size });
    }
    else {
      return iconButton({ iconName, element: def.name, className, dataElementId, title: hint, action, isEnabled, size });
    }
  }
  else {
    return textButton({ element: def.name, className, dataElementId, title: hint, action, isEnabled, size });
  }
};
export const buildElem = ({ element, className, dataElementId, getTitle, action, isEnabled, actions, dataExporterStoreKey, presentationMode, variant, size }) => {
  var _a, _b;
  const title = getTitle(element);
  const propertiesItem = {
    element, className, dataElementId, title, action, isEnabled, actions, variant, size
  };
  switch (element) {
    case TaskbarElement.PREVIOUS:
      return iconButton(Object.assign({ iconName: "chevron-left" }, propertiesItem));
    case TaskbarElement.NEXT:
      return iconButton(Object.assign({ iconName: "chevron-right" }, propertiesItem));
    case TaskbarElement.REFRESH:
      return iconButton(Object.assign({ iconName: "sync" }, propertiesItem));
    case TaskbarElement.UPDATE:
      return iconButton(Object.assign({ iconName: "edit" }, propertiesItem));
    case TaskbarElement.UPDATE_MULTIPLE:
      return iconButton(Object.assign({ iconName: "edit-table" }, propertiesItem));
    case TaskbarElement.CLONE:
      return iconButton(Object.assign({ iconName: "copy" }, propertiesItem));
    case TaskbarElement.REMOVE:
      return iconButton(Object.assign({ iconName: "delete" }, propertiesItem));
    case TaskbarElement.INSERT:
      if (presentationMode === PresentationMode.PRIMARY) {
        return textButton(Object.assign({}, propertiesItem));
      }
      else {
        return iconButton(Object.assign({ iconName: "plus" }, propertiesItem));
      }
    case TaskbarElement.CANCEL:
      return textButton(Object.assign({}, propertiesItem));
    case TaskbarElement.SAVE:
      if (presentationMode === PresentationMode.PRIMARY) {
        return iconTextButton(Object.assign(Object.assign({ iconName: "save" }, propertiesItem), { variant: "primary" }));
      }
      else {
        return iconButton(Object.assign(Object.assign({ iconName: "save" }, propertiesItem), { variant: "primary" }));
      }
    case TaskbarElement.GRID_MODE:
      return iconButton(Object.assign({ iconName: "table" }, propertiesItem));
    case TaskbarElement.FORM_MODE:
      return iconButton(Object.assign({ iconName: "list" }, propertiesItem));
    case TaskbarElement.CONFIGURATOR:
      if ((actions === null || actions === void 0 ? void 0 : actions.length) > 0) {
        return splitButton(Object.assign({ iconName: "settings-inverted" }, propertiesItem));
      }
      return iconButton(Object.assign({ iconName: "settings-inverted" }, propertiesItem));
    case TaskbarElement.MORE_OPTIONS:
      const definedActions = (_a = actions === null || actions === void 0 ? void 0 : actions.filter(a => a !== undefined)) !== null && _a !== void 0 ? _a : [];
      return actionButton(Object.assign(Object.assign({}, propertiesItem), { actions: definedActions }));
    case TaskbarElement.DIVIDER:
      return h("hr", { class: "ez-divider-vertical ez-divider--dark ez-margin--none ez-margin-left--medium", "data-taskbar-divider": true });
    case TaskbarElement.DATA_EXPORTER:
      const provider = (_b = store.get("exporterProviders")) === null || _b === void 0 ? void 0 : _b[dataExporterStoreKey];
      return h("snk-data-exporter", { class: className, provider: provider, "data-element-id": dataElementId, "data-taskbar-element": element, size: size });
    case TaskbarElement.ATTACH:
      return iconButton(Object.assign({ iconName: "anexo" }, propertiesItem));
    case TaskbarElement.ACTIONS_BUTTON:
      return h("snk-actions-button", { "data-element-id": dataElementId, "data-taskbar-element": element, class: className, size: size });
  }
};
function textButton({ action, className, dataElementId, title, element, isEnabled, variant, size }) {
  return (h("ez-tooltip", { key: dataElementId, message: title, class: className }, h("ez-button", { label: title, "data-taskbar-label": title, "data-taskbar-element": element, "data-element-id": dataElementId, isDisabled: !isEnabled(element), onClick: () => action(element), variant: variant, size: size })));
}
function concatClass(className) {
  if (typeof className === "string") {
    return className;
  }
  else {
    return Object.keys(className).filter(key => className[key]).join(" ");
  }
}
function iconButton({ iconName, element, className, dataElementId, title, action, isEnabled, variant, size }) {
  return (h("ez-tooltip", { key: dataElementId, message: title, class: {
      [concatClass(className)]: true,
      [`icon-button--${size}`]: true
    } }, h("ez-button", { mode: "icon", "data-taskbar-label": title, "data-taskbar-element": element, "data-taskbar-icon": iconName, "data-element-id": dataElementId, iconName: iconName, isDisabled: !isEnabled(element), onClick: () => action(element), variant: variant, size: size })));
}
function iconTextButton({ iconName, element, className, dataElementId, title, action, isEnabled, variant, size }) {
  return (h("ez-tooltip", { key: dataElementId, message: title, class: className }, h("ez-button", { label: title, "data-taskbar-label": title, "data-taskbar-icon": iconName, "data-taskbar-element": element, "data-element-id": dataElementId, isDisabled: !isEnabled(element), onClick: () => action(element), variant: variant, size: size }, h("ez-icon", { class: "ez-padding-right--small", slot: "leftIcon", iconName: iconName }))));
}
function splitButton({ iconName, element, className, title, dataElementId, isEnabled, actions, action, size }) {
  return actions && actions.length > 0
    ?
      h("taskbar-split-button", { key: dataElementId, class: className, iconName: iconName, "data-taskbar-icon": iconName, "data-taskbar-label": title, title: title, "data-taskbar-element": element, "data-element-id": dataElementId, enabled: isEnabled(element), actions: actions, action: () => action(element), size: size })
    : undefined;
}
function actionButton({ element, className, dataElementId, title, isEnabled, actions, action, size }) {
  return actions && actions.length > 0
    ? h("taskbar-actions-button", { class: className, key: dataElementId, title: title, "data-taskbar-element": element, "data-element-id": dataElementId, enabled: isEnabled(element), actions: actions, onTaskbarActionSelected: (evt) => action(evt.detail.value), size: size })
    : undefined;
}
