'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-1894343a.js');
const core = require('@sankhyalabs/core');
const utils = require('@sankhyalabs/ezui/dist/collection/utils');
const sortable_esm = require('./sortable.esm-b87669ab.js');
const constants = require('./constants-6d82e405.js');

const guidesConfiguratorCss = ".ezSidebarNavigator.sc-guides-configurator{--ez-sidebar-navigator--height:calc(100vh - 118px)}.navigator-container.sc-guides-configurator{height:calc(100vh - 175px);display:flex;flex-direction:column;justify-content:space-between}.collapsible-container.sc-guides-configurator{display:flex;flex-direction:column;gap:12px;flex-grow:1;max-height:calc(100vh - 240px);overflow:auto}.collapsible-item.sc-guides-configurator{padding:6px;border-radius:6px;border:1px solid #dce0e8}.add-button-container.sc-guides-configurator{display:flex;align-items:center;border-top:1px solid #dce0e8;padding-top:22px;gap:6px;cursor:pointer;justify-content:center}.add-button-container.sc-guides-configurator span.sc-guides-configurator{display:block}.guide-list.sc-guides-configurator{display:flex;flex-direction:column;width:100%;min-height:10px}.guide-list.hidden-empty-content.sc-guides-configurator::before{content:var(--no-hidden-guides-message, \"Nenhuma guia oculta\");display:block;font-size:16px;font-weight:400;color:#AFB6C0;margin:auto;padding:8px}.guide-list.visible-empty-content.sc-guides-configurator::before{content:var(--no-visible-guides-message, \"Nenhuma guia visível\");display:block;font-size:16px;font-weight:400;color:#AFB6C0;margin:auto;padding:8px}.item-label.sc-guides-configurator{display:block;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;flex-grow:1}.guide-item.sc-guides-configurator{margin:0 3px;display:flex;flex-direction:row;align-items:center;justify-content:space-between;padding:5px 10px;border-radius:6px;transition:all 0.2s ease-in-out;height:32px}.guide-item.sc-guides-configurator .actions.sc-guides-configurator{gap:8px;transition:opacity 0.2s ease-in-out;display:none;opacity:0}.guide-item.sc-guides-configurator:hover .actions.sc-guides-configurator{display:flex;opacity:1}ez-button.sc-guides-configurator{width:24px;align-items:center;justify-content:center;display:flex}.ghost.sc-guides-configurator{opacity:.5;background:#e2eaf4}.main-guide.sc-guides-configurator{padding:12px;margin:12px 0;border:1px solid #dce0e8;border-radius:6px;transition:all 0.2s ease-in-out}.guide-item.selected.sc-guides-configurator,.main-guide.selected.sc-guides-configurator{background:#E2F4EF}.guide-item.sc-guides-configurator:hover,.main-guide.sc-guides-configurator:hover{background:#f5fcfa}";

const GuidesConfigurator = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.guideSelected = index.createEvent(this, "guideSelected", 7);
    this.guideListChanged = index.createEvent(this, "guideListChanged", 7);
    this.guideDeleted = index.createEvent(this, "guideDeleted", 7);
    this.createNewGuide = index.createEvent(this, "createNewGuide", 7);
    this.ID_VISIBLE_GUIDES = 'container-visible-guides';
    this.ID_HIDDEN_GUIDES = 'container-hidden-guides';
    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.mainGuide = undefined;
    this.filterTerm = '';
    this.visibleGuides = [];
    this.hiddenGuides = [];
    this.guidesList = [];
    this.selectedGuide = undefined;
    this.messagesBuilder = undefined;
  }
  onMessagesBuilderChange() {
    this.setCustomCSSProperties();
  }
  observeVisibleGuides(newGuides) {
    this.initGuidesConfig(newGuides);
  }
  observeSelectedGuide(newGuide) {
    this.guideSelected.emit(newGuide);
  }
  getMessage(key, params) {
    return this.messagesBuilder.getMessage(key, params);
  }
  initGuidesConfig(newGuides) {
    const visibleAux = [...newGuides.filter(guide => (guide.visible && guide.name !== constants.TAB_NAMES.main))];
    const hiddenAux = [...newGuides.filter(guide => (!guide.visible && guide.name !== constants.TAB_NAMES.main))];
    this.visibleGuides = [...visibleAux.sort(this.sortLogic)];
    this.hiddenGuides = [...hiddenAux.sort(this.sortLogic)];
    this.mainGuide = newGuides.find(g => g.name === constants.TAB_NAMES.main);
  }
  handleAddGuide() {
    this.createNewGuide.emit();
  }
  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)];
    }
    const updatedGuideList = [...visibleAux, ...hiddenAux];
    this.applyGuidesUpdate(updatedGuideList);
  }
  adjustListOrder(guideList) {
    return guideList.map((g, index) => {
      return Object.assign(Object.assign({}, g), { order: index });
    });
  }
  applyGuidesUpdate(updatedGuideList) {
    let newGuidesList = this.adjustListOrder(updatedGuideList);
    newGuidesList = [this.mainGuide, ...newGuidesList];
    if (!core.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 utils.ApplicationUtils.confirm(title, message);
    if (!canRemove)
      return;
    this.guideDeleted.emit(guide);
    const deletedMessage = this.getMessage('snkFormConfig.info.successfullyGuideDeleted').replace('{0}', guide.label);
    utils.ApplicationUtils.info(deletedMessage);
  }
  renderItems(guides) {
    const guidesToShow = core.ArrayUtils.applyStringFilter(this.filterTerm, guides);
    return guidesToShow.map(guide => {
      var _a;
      return (index.h("div", { key: guide.name, "data-guide-name": guide.name, title: guide.label, class: `guide-item ${((_a = this.selectedGuide) === null || _a === void 0 ? void 0 : _a.label) === guide.label ? 'selected' : ''} ${this.filterTerm.length ? 'disable-sort' : ''}`, onClick: e => this.handleSelectGuide(guide, e) }, index.h("span", null, index.h("ez-icon", { iconName: "drag-indicator", class: 'ez-margin-right--small' })), index.h("span", { class: 'item-label' }, guide.label), index.h("div", { class: 'actions' }, index.h("ez-button", { mode: "icon", class: "ez-button--tertiary", onClick: e => this.toggleGuideVisibility(guide, e), size: "medium", iconName: guide.visible ? 'eye-off' : 'eye', title: guide.visible ?
          this.getMessage('snkFormConfig.guidesConfigurator.actions.hide') :
          this.getMessage('snkFormConfig.guidesConfigurator.actions.show') }), index.h("ez-button", { mode: "icon", class: "ez-button--tertiary", onClick: e => this.handleDeleteGuide(guide, e), size: "medium", isDisabled: guide.isCustom || guide.isDetail, iconName: 'delete', title: guide.isCustom || guide.isDetail ?
          this.getMessage('snkFormConfig.guidesConfigurator.canNotDeleteCustomGuide') :
          this.getMessage('snkFormConfig.guidesConfigurator.actions.delete') }))));
    });
  }
  getContainerElement(id) {
    return this.el.querySelector(`[id="${id}"]`);
  }
  handleSortGuide() {
    const updatedGuideList = [...this.getGuidesFromStatus(true), ...this.getGuidesFromStatus(false)];
    this.applyGuidesUpdate(updatedGuideList);
  }
  getGuidesFromStatus(visible) {
    let updatedGuideList = [];
    const orderedGuides = this.getOrderedGuideNames(visible ? this.ID_VISIBLE_GUIDES : this.ID_HIDDEN_GUIDES);
    orderedGuides.forEach(guideName => {
      const updatedGuide = this.guidesList.find(g => g.name === guideName);
      if (updatedGuide) {
        updatedGuideList.push(Object.assign(Object.assign({}, updatedGuide), { visible }));
      }
    });
    return updatedGuideList;
  }
  getOrderedGuideNames(containerId) {
    var _a;
    const guidesCollection = (_a = this.getContainerElement(containerId)) === null || _a === void 0 ? void 0 : _a.children;
    return Array.from(guidesCollection).map((element) => element.getAttribute('data-guide-name'));
  }
  initializeDraggableContainers() {
    this._sortableVisibleGuides = new sortable_esm.Sortable(this.getContainerElement(this.ID_VISIBLE_GUIDES), {
      group: 'guides',
      ghostClass: 'ghost',
      direction: 'vertical',
      filter: '.disable-sort',
      animation: 15,
      onSort: () => this.handleSortGuide(),
    });
    this._sortableInvisibleGuides = new sortable_esm.Sortable(this.getContainerElement(this.ID_HIDDEN_GUIDES), {
      group: 'guides',
      ghostClass: 'ghost',
      animation: 15,
      filter: '.disable-sort',
      onSort: () => this.handleSortGuide(),
    });
  }
  handleSelectMainGuide() {
    this.guideSelected.emit(this.mainGuide);
  }
  setCustomCSSProperties() {
    document.documentElement.style.setProperty('--no-hidden-guides-message', `"${this.getMessage('snkFormConfig.guidesConfigurator.noHiddenGuides')}"`);
    document.documentElement.style.setProperty('--no-visible-guides-message', `"${this.getMessage('snkFormConfig.guidesConfigurator.noVisibleGuides')}"`);
  }
  componentDidLoad() {
    this.initializeDraggableContainers();
    this.initGuidesConfig(this.guidesList);
    this.setCustomCSSProperties();
  }
  render() {
    var _a, _b, _c;
    return (index.h("ez-sidebar-navigator", { class: 'ezSidebarNavigator', type: 'fixed', size: 'md', titleMenu: this.getMessage('snkFormConfig.guidesConfigurator.title') }, index.h("div", { class: 'navigator-container' }, index.h("ez-filter-input", { slot: "content", mode: "slim", label: this.getMessage('snkFormConfig.guidesConfigurator.searchGuide'), onEzChange: evt => this.filterTerm = evt.detail }), index.h("div", { onClick: this.handleSelectMainGuide.bind(this), class: `main-guide ${((_a = this.selectedGuide) === null || _a === void 0 ? void 0 : _a.name) === ((_b = this.mainGuide) === null || _b === void 0 ? void 0 : _b.name) ? 'selected' : ''}` }, index.h("span", null, (_c = this.mainGuide) === null || _c === void 0 ? void 0 : _c.label)), index.h("div", { class: 'collapsible-container' }, index.h("div", { class: 'collapsible-item' }, index.h("ez-collapsible-box", { label: this.getMessage('snkFormConfig.guidesConfigurator.visibleGuides'), headerSize: "large" }, index.h("div", { id: this.ID_VISIBLE_GUIDES, class: `guide-list ${!this.visibleGuides.length ? 'visible-empty-content' : ''}` }, this.renderItems(this.visibleGuides)))), index.h("div", { class: 'collapsible-item' }, index.h("ez-collapsible-box", { label: this.getMessage('snkFormConfig.guidesConfigurator.hiddenGuides'), headerSize: "large" }, index.h("div", { id: this.ID_HIDDEN_GUIDES, class: `guide-list ${!this.hiddenGuides.length ? 'hidden-empty-content' : ''}` }, this.renderItems(this.hiddenGuides))))), index.h("div", { class: 'add-button-container', onClick: this.handleAddGuide.bind(this) }, index.h("ez-icon", { slot: "leftIcon", iconName: "plus" }), index.h("span", null, this.getMessage('snkFormConfig.guidesConfigurator.createNewGuide'))))));
  }
  get el() { return index.getElement(this); }
  static get watchers() { return {
    "messagesBuilder": ["onMessagesBuilderChange"],
    "guidesList": ["observeVisibleGuides"],
    "selectedGuide": ["observeSelectedGuide"]
  }; }
};
GuidesConfigurator.style = guidesConfiguratorCss;

exports.guides_configurator = GuidesConfigurator;
