import { r as registerInstance, e as createEvent, h, g as getElement } from './index-e9c7ceaa.js';
import { ApplicationContext } from '@sankhyalabs/core';
import { V as VIEW_MODE } from './constants-7b422de0.js';
import { A as AuthorizationConfig } from './AuthorizationConfig-dcbd207a.js';

const snkConfiguratorCss = ".sc-snk-configurator-h{--snk-configurator__button-close--padding-left:var(--space--medium, 12px);--snk-configurator__title--color:var(--title--primary, #2B3A54);--snk-configurator__title--font-family:var(--font-pattern, \"Roboto\");--snk-configurator__title--font-size:var(--title--large, 20px);--snk-configurator__title--font-weight:var(--text-weight--extra-large, 700);--snk-configurator__subtitle--color:var(--title--primary, #2B3A54);--snk-configurator__subtitle--font-family:var(--font-pattern, \"Roboto\");--snk-configurator__subtitle--font-size:var(--text--medium, 14px);--snk-configurator__subtitle--font-weight:var(--text-weight--large, 600);--snk-configurator__scrollbar--color-default:var(--scrollbar--default, #626e82);--snk-configurator__scrollbar--color-background:var(--scrollbar--background, #E5EAF0);--snk-configurator__scrollbar--color-hover:var(--scrollbar--hover, #2B3A54);--snk-configurator__scrollbar--color-clicked:var(--scrollbar--clicked, #a2abb9);--snk-configurator__scrollbar--border-radius:var(--border--radius-small, 6px);--snk-configurator__scrollbar--width:var(--space--small, 6px)}.snk-configurator.sc-snk-configurator{display:grid;grid-template-rows:auto 1fr auto;max-height:100%;width:100%}.snk-configurator__collsable-box-action.sc-snk-configurator{width:100%}.snk-configurator__action-buttons.sc-snk-configurator{display:flex;flex-direction:row;justify-content:flex-end;gap:var(--space--medium)}.snk-configurator__outline.sc-snk-configurator{display:flex;flex-direction:column;gap:var(--space--small);margin-top:var(--space--small);margin-bottom:var(--space--large)}@media screen and (min-width: 480px){.snk-configurator.sc-snk-configurator{width:359px;max-width:359px}}.snk-configurator__button-config.sc-snk-configurator{--ez-button--min-width:100%}.snk-configurator__header.sc-snk-configurator{display:flex;justify-content:space-between;align-items:center;width:100%}.snk-configurator__title.sc-snk-configurator{display:flex;margin:0;line-height:normal;letter-spacing:0em;text-align:left;color:var(--snk-configurator__title--color);font-size:var(--snk-configurator__title--font-size);font-family:var(--snk-configurator__title--font-family);font-weight:var(--snk-configurator__title--font-weight)}.snk-configurator__subtitle.sc-snk-configurator{display:flex;margin:0;padding-top:var(--space--medium);padding-bottom:var(--space--small);color:var(--snk-configurator__subtitle--color);font-size:var(--snk-configurator__subtitle--font-size);font-family:var(--snk-configurator__subtitle--font-family);font-weight:var(--snk-configurator__subtitle--font-weight)}.snk-configurator__button-close.sc-snk-configurator{padding-left:var(--snk-configurator__button-close--padding-left)}.snk-configurator__main.sc-snk-configurator{overflow-y:auto;scrollbar-width:thin;margin:var(--space--medium) 0;scrollbar-color:var(--snk-configurator__scrollbar--color-clicked) var(--snk-configurator__scrollbar--color-background)}.snk-configurator__main.sc-snk-configurator::-webkit-scrollbar{background-color:var(--snk-configurator__scrollbar--color-background);width:var(--snk-configurator__scrollbar--width);max-width:var(--snk-configurator__scrollbar--width);min-width:var(--snk-configurator__scrollbar--width)}.snk-configurator__main.sc-snk-configurator::-webkit-scrollbar-track{background-color:var(--snk-configurator__scrollbar--color-background);border-radius:var(--snk-configurator__scrollbar--border-radius)}.snk-configurator__main.sc-snk-configurator::-webkit-scrollbar-thumb{background-color:var(--snk-configurator__scrollbar--color-default);border-radius:var(--snk-configurator__scrollbar--border-radius)}.snk-configurator__main.sc-snk-configurator::-webkit-scrollbar-thumb:vertical:hover,.snk-configurator__main.sc-snk-configurator::-webkit-scrollbar-thumb:horizontal:hover{background-color:var(--snk-configurator__scrollbar--color-hover)}.snk-configurator__main.sc-snk-configurator::-webkit-scrollbar-thumb:vertical:active,.snk-configurator__main.sc-snk-configurator::-webkit-scrollbar-thumb:horizontal:active{background-color:var(--snk-configurator__scrollbar--color-clicked)}";

const SnkConfigurator = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.configSelected = createEvent(this, "configSelected", 7);
    this.openConfig = createEvent(this, "openConfig", 7);
    this.save = createEvent(this, "save", 7);
    this.cancel = createEvent(this, "cancel", 7);
    this._opened = false;
    this._permissions = undefined;
    this.showActionButtons = false;
    this.configName = undefined;
    this.resourceID = undefined;
    this.viewMode = VIEW_MODE.GRID;
    this.customContainerId = undefined;
    this.layoutFormConfig = true;
    this.messagesBuilder = undefined;
  }
  /**
   * Exibe a janela de configuração.
   */
  async open() {
    this._opened = true;
    this.configSelected.emit(this.getViewModeValue());
    this.modal.heightMode = 'full';
  }
  /**
   * Fecha a janela de configuração.
   */
  async close() {
    this.closeConfig();
  }
  closeConfig() {
    this._opened = false;
  }
  onViewModeChanged(evt) {
    const value = evt === null || evt === void 0 ? void 0 : evt.detail;
    this.viewMode = value;
    this.configSelected.emit(this.viewMode);
  }
  onClickOpenConfig() {
    this.openConfig.emit(this.viewMode);
  }
  isAllowed(configName) {
    if (AuthorizationConfig[configName]) {
      return this._permissions ? this._permissions.isSup || this._permissions[configName] : false;
    }
    return true;
  }
  enableModeConfig() {
    if (!this.isAllowed(AuthorizationConfig.CONFIG_GRID) || !this.isAllowed(AuthorizationConfig.CONFIG)) {
      return false;
    }
    return true;
  }
  getViewModeValue() {
    if (this._permissions != undefined) {
      if (!this.isAllowed(AuthorizationConfig.CONFIG_GRID)) {
        this.viewMode = VIEW_MODE.FORM;
      }
      else if (!this.isAllowed(AuthorizationConfig.CONFIG)) {
        this.viewMode = VIEW_MODE.GRID;
      }
    }
    return this.viewMode;
  }
  /**
   * Conforme mecanismo de mensagens, é possível customizar as mensagens dos blocos de construção
   * através de um pequeno modulo na estrutura da aplicação:
   * - Criar um arquivo no seguinte caminho: /messages/appmessages.msg.js.
   * Para conhecer os detalhes do módulo, vide o arquivo neste projeto "/src/lib/message/resources/snk-configurator.msg.ts"
   */
  getMessage(key) {
    var _a;
    return (_a = this.messagesBuilder) === null || _a === void 0 ? void 0 : _a.getMessage(key, {});
  }
  handleSave() {
    if (this.layoutFormConfig) {
      this._snkLayoutFormConfig.save();
    }
    this.save.emit();
  }
  componentWillLoad() {
    this._application = ApplicationContext.getContextValue('__SNK__APPLICATION__');
    if (this._application) {
      this._application.getAllAccess(this.resourceID).then(access => (this._permissions = access));
    }
    else {
      this._permissions = {};
    }
  }
  componentDidRender() {
    if (!this.customContainerId)
      return;
    const container = document.querySelector(`#${this.customContainerId}`);
    const customElements = container === null || container === void 0 ? void 0 : container.querySelector('#SnkConfigContainerSlot');
    if (!customElements)
      return;
    const configuratorCustomElement = customElements.firstElementChild;
    if (!configuratorCustomElement)
      return;
    const insideCustomElementsContainer = this._element.querySelector("#slotContainer");
    insideCustomElementsContainer === null || insideCustomElementsContainer === void 0 ? void 0 : insideCustomElementsContainer.appendChild(configuratorCustomElement);
  }
  render() {
    return (h("ez-modal", { ref: (element) => (this.modal = element), opened: this._opened, "close-esc": false, "close-outside-click": false, "modal-size": "small", scrim: "light", onEzCloseModal: () => this.closeConfig() }, h("div", { class: "snk-configurator" }, h("div", { class: "snk-configurator__header" }, h("label", { class: "snk-configurator__title" }, this.getMessage('snkConfigurator.titleConfigurations')), h("div", { class: "snk-configurator__button-close" }, h("ez-button", { mode: "icon", iconName: "close", size: "medium", onClick: () => this.closeConfig() }))), h("div", { class: "snk-configurator__main" }, h("ez-collapsible-box", { boxBordered: true, label: this.getMessage('snkConfigurator.subTitleModeConfig'), headerSize: "medium", class: "snk-configurator__collapsable-box" }, h("ez-radio-button", { value: this.getViewModeValue().toString(), onEzChange: (evt) => this.onViewModeChanged(evt), enabled: this.enableModeConfig() }, h("ez-radio-button-option", { label: this.getMessage('snkConfigurator.labelGrid'), value: VIEW_MODE.GRID }), h("ez-radio-button-option", { label: this.getMessage('snkConfigurator.labelForm'), value: VIEW_MODE.FORM })), h("div", { class: "snk-configurator__collsable-box-action" }, h("ez-button", { label: this.viewMode === VIEW_MODE.GRID ? this.getMessage('snkConfigurator.labelConfigGrid') : this.getMessage('snkConfigurator.labelConfigForm'), class: "snk-configurator__button-config ez-button--secondary", onClick: () => this.onClickOpenConfig(), id: "openConfigurator" }))), this.layoutFormConfig && h("ez-collapsible-box", { boxBordered: true, value: true, label: this.getMessage('snkConfigurator.labelLayoutForm'), headerSize: "medium", class: "snk-configurator__collapsable-box  ez-margin-top--medium" }, h("div", { class: 'ez-flex ez-flex--column ez-size-width--full' }, h("snk-layout-form-config", { ref: (ref) => this._snkLayoutFormConfig = ref, messagesBuilder: this.messagesBuilder }))), h("div", { id: "slotContainer", class: "snk-configurator__outline" })), this.showActionButtons && (h("div", { class: "snk-configurator__action-buttons" }, h("ez-button", { label: this.getMessage('snkConfigurator.buttons.cancel'), onClick: () => this.cancel.emit() }), h("ez-button", { label: this.getMessage('snkConfigurator.buttons.save'), onClick: () => this.handleSave(), class: "ez-button--primary" }))))));
  }
  get _element() { return getElement(this); }
};
SnkConfigurator.style = snkConfiguratorCss;

export { SnkConfigurator as snk_configurator };
