import { proxyCustomElement, HTMLElement, createEvent, h } from '@stencil/core/internal/client';
import { ObjectUtils } from '@sankhyalabs/core';
import { ApplicationUtils } from '@sankhyalabs/ezui/dist/collection/utils';
import { T as TAB_NAMES } from './constants.js';

const snkDynaformGuidesConfiguratorCss = ".guides-tab-configurator.sc-snk-dynaform-guides-configurator{display:flex;flex-direction:column;width:100%;gap:var(--spece--8, 8px)}.tab-bar-row.sc-snk-dynaform-guides-configurator{display:flex;flex-direction:row;align-items:center;gap:var(--space--12, 12px);width:100%}.guides-tabselector.sc-snk-dynaform-guides-configurator{flex:1;min-width:0}.tab-actions.sc-snk-dynaform-guides-configurator{display:inline-flex;align-items:center;gap:var(--space--2, 2px);margin-left:var(--space--6, 6px)}.tab-actions.sc-snk-dynaform-guides-configurator ez-button.sc-snk-dynaform-guides-configurator{width:var(--space--20, 20px);align-items:center;justify-content:center;display:flex}.hidden-guides-bar.sc-snk-dynaform-guides-configurator{display:flex;flex-direction:row;flex-wrap:wrap;align-items:center;gap:var(--space--8, 8px);padding:var(--space--8, 8px) 0;border-top:1px solid #dce0e8}.hidden-guides-label.sc-snk-dynaform-guides-configurator{font-size:var(--space--12, 12px);color:#afb6c0;white-space:nowrap}.hidden-guide-chip.sc-snk-dynaform-guides-configurator{display:flex;align-items:center;gap:4px;padding:2px var(--space--8, 8px);border:1px solid #dce0e8;border-radius:var(--space--12, 12px);background:#f5f7fa;transition:background 0.15s ease-in-out}.hidden-guide-chip.sc-snk-dynaform-guides-configurator:hover{background:#e2eaf4}.hidden-guide-chip.sc-snk-dynaform-guides-configurator ez-button.sc-snk-dynaform-guides-configurator{width:var(--space--20, 20px);align-items:center;justify-content:center;display:flex}.chip-label.sc-snk-dynaform-guides-configurator{font-size:13px;max-width:120px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;cursor:pointer}";

const SnkDynaformGuidesConfigurator = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    this.guideSelected = createEvent(this, "guideSelected", 7);
    this.guideListChanged = createEvent(this, "guideListChanged", 7);
    this.guideDeleted = createEvent(this, "guideDeleted", 7);
    this.createNewGuide = createEvent(this, "createNewGuide", 7);
    this.sortLogic = (a, b) => {
      var _a, _b;
      const orderA = (_a = a.order) !== null && _a !== void 0 ? _a : Number.MAX_VALUE;
      const orderB = (_b = b.order) !== null && _b !== void 0 ? _b : Number.MAX_VALUE;
      return orderA - orderB;
    };
    this.handleAddGuide = () => {
      this.createNewGuide.emit();
    };
    this.mainGuide = undefined;
    this.visibleGuides = [];
    this.hiddenGuides = [];
    this.guidesList = [];
    this.selectedGuide = undefined;
    this.messagesBuilder = undefined;
  }
  observeGuidesList(newGuides) {
    this.initGuidesConfig(newGuides);
  }
  observeSelectedGuide(newGuide) {
    this.guideSelected.emit(newGuide);
  }
  componentWillLoad() {
    this.initGuidesConfig(this.guidesList);
  }
  getMessage(key, params) {
    return this.messagesBuilder.getMessage(key, params);
  }
  initGuidesConfig(newGuides) {
    const visibleAux = newGuides.filter(guide => guide.visible && guide.name !== TAB_NAMES.main);
    const hiddenAux = newGuides.filter(guide => !guide.visible && guide.name !== TAB_NAMES.main);
    this.visibleGuides = [...visibleAux].sort(this.sortLogic);
    this.hiddenGuides = [...hiddenAux].sort(this.sortLogic);
    this.mainGuide = newGuides.find(g => g.name === TAB_NAMES.main);
  }
  handleSelectGuide(guide, evt) {
    evt.stopPropagation();
    this.selectedGuide = guide;
  }
  toggleGuideVisibility(guide, evt) {
    evt.stopPropagation();
    const updatedGuide = Object.assign(Object.assign({}, guide), { visible: !guide.visible });
    let visibleAux;
    let hiddenAux;
    if (guide.visible) {
      visibleAux = this.visibleGuides.filter(g => g.name !== guide.name);
      hiddenAux = [...this.hiddenGuides, updatedGuide];
    }
    else {
      visibleAux = [...this.visibleGuides, updatedGuide];
      hiddenAux = this.hiddenGuides.filter(g => g.name !== guide.name);
    }
    this.applyGuidesUpdate([...visibleAux, ...hiddenAux]);
  }
  adjustListOrder(guideList) {
    return guideList.map((g, index) => (Object.assign(Object.assign({}, g), { order: index })));
  }
  applyGuidesUpdate(updatedGuideList) {
    let newGuidesList = this.adjustListOrder(updatedGuideList);
    if (this.mainGuide) {
      newGuidesList = [this.mainGuide, ...newGuidesList];
    }
    if (!ObjectUtils.equals(newGuidesList, this.guidesList)) {
      this.guideListChanged.emit(newGuidesList);
    }
  }
  async handleDeleteGuide(guide, e) {
    e.stopPropagation();
    const title = this.getMessage('snkFormConfig.confirm.title');
    const message = this.getMessage('snkFormConfig.confirm.deleteGuide').replace('{0}', guide.label);
    const canRemove = await ApplicationUtils.confirm(title, message);
    if (!canRemove)
      return;
    this.guideDeleted.emit(guide);
    const deletedMessage = this.getMessage('snkFormConfig.info.successfullyGuideDeleted').replace('{0}', guide.label);
    ApplicationUtils.info(deletedMessage);
  }
  buildTabs() {
    const tabs = [];
    if (this.mainGuide) {
      tabs.push({ tabKey: this.mainGuide.name, label: this.mainGuide.label, index: 0 });
    }
    this.visibleGuides.forEach((guide, i) => {
      tabs.push({
        tabKey: guide.name,
        label: guide.label,
        index: (this.mainGuide ? 1 : 0) + i,
      });
    });
    return tabs;
  }
  handleTabChange(tab) {
    var _a;
    if (!tab)
      return;
    const guide = this.guidesList.find(g => g.name === tab.tabKey);
    if (guide && guide.name !== ((_a = this.selectedGuide) === null || _a === void 0 ? void 0 : _a.name)) {
      this.selectedGuide = guide;
    }
  }
  handleReorder(reorderedTabs) {
    // __main fica fixo no índice 0; se o usuário tentou movê-lo, recolocar
    const visibleReordered = reorderedTabs
      .filter(t => { var _a; return t.tabKey !== ((_a = this.mainGuide) === null || _a === void 0 ? void 0 : _a.name); })
      .map(t => this.visibleGuides.find(g => g.name === t.tabKey))
      .filter(Boolean);
    this.applyGuidesUpdate([...visibleReordered, ...this.hiddenGuides]);
  }
  renderTabActions() {
    return this.visibleGuides.map((guide, i) => {
      const slotIndex = (this.mainGuide ? 1 : 0) + i;
      const isProtected = guide.isCustom || guide.isDetail;
      return (h("div", { key: guide.name, slot: `tab${slotIndex}`, class: "tab-actions", onClick: (e) => e.stopPropagation() }, h("ez-button", { mode: "icon", class: "ez-button--tertiary tab-action-btn", size: "small", iconName: "eye-off", title: this.getMessage('snkFormConfig.guidesConfigurator.actions.hide'), onClick: (e) => this.toggleGuideVisibility(guide, e) }), h("ez-button", { mode: "icon", class: "ez-button--tertiary tab-action-btn", size: "small", iconName: "delete", isDisabled: isProtected, title: isProtected
          ? this.getMessage('snkFormConfig.guidesConfigurator.canNotDeleteCustomGuide')
          : this.getMessage('snkFormConfig.guidesConfigurator.actions.delete'), onClick: (e) => this.handleDeleteGuide(guide, e) })));
    });
  }
  renderHiddenGuidesBar() {
    if (this.hiddenGuides.length === 0)
      return null;
    return (h("div", { class: "hidden-guides-bar" }, h("span", { class: "hidden-guides-label" }, this.getMessage('snkFormConfig.guidesConfigurator.hiddenGuides'), ":"), this.hiddenGuides.map(guide => {
      const isProtected = guide.isCustom || guide.isDetail;
      return (h("div", { key: guide.name, class: "hidden-guide-chip", title: guide.label }, h("span", { class: "chip-label", onClick: (e) => this.handleSelectGuide(guide, e) }, guide.label), h("ez-button", { mode: "icon", class: "ez-button--tertiary", size: "small", iconName: "eye", title: this.getMessage('snkFormConfig.guidesConfigurator.actions.show'), onClick: (e) => this.toggleGuideVisibility(guide, e) }), h("ez-button", { mode: "icon", class: "ez-button--tertiary", size: "small", iconName: "delete", isDisabled: isProtected, title: isProtected
          ? this.getMessage('snkFormConfig.guidesConfigurator.canNotDeleteCustomGuide')
          : this.getMessage('snkFormConfig.guidesConfigurator.actions.delete'), onClick: (e) => this.handleDeleteGuide(guide, e) })));
    })));
  }
  render() {
    var _a;
    const tabs = this.buildTabs();
    return (h("div", { class: "guides-tab-configurator" }, h("div", { class: "tab-bar-row" }, h("ez-tabselector", { class: "guides-tabselector", tabs: tabs, selectedTab: (_a = this.selectedGuide) === null || _a === void 0 ? void 0 : _a.name, reorderable: true, onEzChange: (evt) => this.handleTabChange(evt.detail), onEzReorder: (evt) => this.handleReorder(evt.detail) }, this.renderTabActions()), h("ez-button", { label: this.getMessage('snkFormConfig.guidesConfigurator.createNewGuide'), variant: "secondary", size: "medium", leftIconName: "plus", onClick: this.handleAddGuide })), this.renderHiddenGuidesBar()));
  }
  static get watchers() { return {
    "guidesList": ["observeGuidesList"],
    "selectedGuide": ["observeSelectedGuide"]
  }; }
  static get style() { return snkDynaformGuidesConfiguratorCss; }
}, [2, "snk-dynaform-guides-configurator", {
    "guidesList": [16],
    "selectedGuide": [16],
    "messagesBuilder": [1040],
    "mainGuide": [32],
    "visibleGuides": [32],
    "hiddenGuides": [32]
  }]);
function defineCustomElement() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["snk-dynaform-guides-configurator"];
  components.forEach(tagName => { switch (tagName) {
    case "snk-dynaform-guides-configurator":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, SnkDynaformGuidesConfigurator);
      }
      break;
  } });
}

export { SnkDynaformGuidesConfigurator as S, defineCustomElement as d };
