import { proxyCustomElement, HTMLElement, createEvent, forceUpdate, h, Host } from '@stencil/core/internal/client';
import { StringUtils, ObjectUtils } from '@sankhyalabs/core';
import { ApplicationUtils } from '@sankhyalabs/ezui/dist/collection/utils';
import { S as Sortable } from './sortable.esm.js';
import { T as TAB_NAMES, G as GROUP_NAMES } from './constants.js';
import { S as SORTABLE_PATH, a as getFieldsStructure, d as defineCustomElement$2, I as ID_AVAILABLE_FIELDS } from './field-config2.js';
import { d as defineCustomElement$1 } from './field-item2.js';

const fieldsLayoutCss = ".sc-fields-layout-h{display:flex;padding:12px;box-shadow:var(--shadow--small, 0 0 16px rgba(0, 38, 111, 0.07));border-radius:var(--border--radius-medium, 6px);height:var(--snk-form-config-container-height);flex-direction:column}.layout-container.sc-fields-layout{container-type:inline-size;display:flex;flex-direction:column;overflow:auto;height:calc(100vh - 225px)}.guide-header.sc-fields-layout{display:flex;width:100%;font-size:20px;border-bottom:1px solid #dce0e8;margin-bottom:6px;align-items:center;height:45px}.guide-header.sc-fields-layout ez-button.sc-fields-layout{transition:all 0.2s ease-in-out;visibility:hidden;opacity:0}.guide-header.sc-fields-layout:hover ez-button.sc-fields-layout{visibility:visible;opacity:1}.guide-header-label.sc-fields-layout{display:block;padding:10px 0}.group-config.sc-fields-layout{padding:12px 0;display:flex;flex-direction:column;width:100%}.group-content.sc-fields-layout{display:grid;gap:12px;min-height:65px;width:100%;padding:6px 0;grid-template-columns:repeat(auto-fill, minmax(250px, 1fr))}.group-content.empty-content.sc-fields-layout{position:relative}.group-content.empty-content.sc-fields-layout::before{content:var(--empty-content-message, \"Arraste e solte um campo aqui\");color:#cbcfd7;font-size:16px;font-weight:500;text-align:center;position:absolute;inset:0;display:flex;align-items:center;justify-content:center;pointer-events:none;transition:opacity 0.3s;height:100%;box-sizing:border-box;padding:10px;background:#f9f9f9;border:1px dashed #dce0e8;border-radius:6px}.no-group-container.sc-fields-layout{padding:12px 0;width:100%;border-bottom:1px solid #dce0e8}.no-group-container.sc-fields-layout::before{content:var(--no-group-message, \"Inclua estes campos nos grupos ou deixe-os separados no topo do formulário\");width:100%;display:block;font-size:16px;font-weight:400;color:#AFB6C0;padding-bottom:12px}.add-group-container.sc-fields-layout{display:flex;align-items:center;border-top:1px solid #dce0e8;padding-top:22px;padding-bottom:12px;gap:6px;cursor:pointer;justify-content:center;width:100%;height:55px}.no-guide-selected.sc-fields-layout{display:block;margin:auto;font-size:24px;color:#AFB6C0;font-weight:500}.message-container.sc-fields-layout{margin:auto;display:flex;flex-direction:column;justify-content:center;align-items:center}.message-config-detail.sc-fields-layout{display:block;margin:auto;font-size:var(--font-size--medium, 16px);color:#AFB6C0;font-weight:var(--font-weight--medium, 500)}.ghost.sc-fields-layout{opacity:.5;background:#c8fbe2}";

const FieldLayoutComponent = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  constructor() {
    super();
    this.__registerHost();
    this.isEditingGuideName = createEvent(this, "isEditingGuideName", 7);
    this.isEditingGroupName = createEvent(this, "isEditingGroupName", 7);
    this.layoutChanged = createEvent(this, "layoutChanged", 7);
    this.setFieldListAsAvailable = createEvent(this, "setFieldListAsAvailable", 7);
    this.guideRenamed = createEvent(this, "guideRenamed", 7);
    this.ID_GROUPS = 'container-groups';
    this.ID_FIELDS = 'container-fields';
    this.reservedGuideNames = [TAB_NAMES.main.toLowerCase(), TAB_NAMES.general.toLocaleLowerCase()];
    this.groupsListMemory = [];
    this.groupsRefList = [];
    this.isEditGuideNameActive = false;
    this.selectedGuide = undefined;
    this.groupsList = [];
    this.guideNames = [];
    this.dataUnit = undefined;
    this.messagesBuilder = undefined;
  }
  onMessagesBuilderChange() {
    this.setCustomCSSProperties();
  }
  handleFieldConfigChanged({ detail: updatedField }) {
    const groupToUpdate = this.groupsListMemory.find(g => g.name === updatedField.group);
    if (!groupToUpdate)
      return;
    const updatedFields = groupToUpdate.fields.map(field => {
      return (field.name === updatedField.name) ? updatedField : field;
    });
    const updatedGroupList = this.groupsListMemory.map(group => {
      return (group.name === groupToUpdate.name)
        ? { name: group.name, fields: updatedFields }
        : group;
    });
    this.applyGroupUpdate(updatedGroupList);
  }
  observeSelectedGuide() {
    this.isEditGuideNameActive = false;
    forceUpdate(this);
    this.isEditingGroupName.emit(false);
  }
  observeGroupList(newValue) {
    this.groupsListMemory = [...newValue];
  }
  observeIsEditGuideNameActive(newValue) {
    this.isEditingGuideName.emit(newValue);
  }
  async addFieldToLayout(incomingField) {
    incomingField.group = GROUP_NAMES.noGroup;
    incomingField.tab = this.selectedGuide.name;
    const fixedFieldsGroup = this.getFixedFieldsGroup();
    const newFixedFieldsGroup = {
      name: GROUP_NAMES.noGroup,
      fields: fixedFieldsGroup ? [...fixedFieldsGroup.fields, incomingField] : [incomingField],
    };
    const updatedGroupListMemory = fixedFieldsGroup
      ? [newFixedFieldsGroup, ...this.groupsListMemory.slice(1)]
      : [newFixedFieldsGroup, ...this.groupsListMemory];
    this.applyGroupUpdate(updatedGroupListMemory);
  }
  getFixedFieldsGroup() {
    return this.groupsListMemory.find(g => g.name === GROUP_NAMES.noGroup);
  }
  getMessage(key, params) {
    return this.messagesBuilder.getMessage(key, params);
  }
  handleAddGroup() {
    const newGroup = { name: this.buildNewGroupName(), fields: [] };
    const updatedGroupsList = [...this.groupsListMemory, newGroup];
    this.applyGroupUpdate(updatedGroupsList);
  }
  buildNewGroupName() {
    var _a, _b;
    const newGroupMessage = this.getMessage('snkFormConfig.form.newGroup');
    const qtdWithNewGroupName = (_b = (_a = this.groupsListMemory.filter(g => g.name.includes(newGroupMessage))) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
    return qtdWithNewGroupName === 0 ? newGroupMessage : `${newGroupMessage} (${qtdWithNewGroupName})`;
  }
  makeDynamicFieldsContainersDraggable() {
    const draggableGroups = this.groupsList.filter((g) => g.name !== GROUP_NAMES.noGroup);
    draggableGroups.forEach(group => {
      this.makeFieldContainerDraggable(this.getFieldsContainerId(group.name));
    });
  }
  getFieldsContainerId(groupName) {
    return `${this.ID_FIELDS}-${StringUtils.replaceBlankCharacters(groupName)}`;
  }
  handleSortGroups(evt) {
    var _a;
    const htmlStructure = (_a = evt.to) === null || _a === void 0 ? void 0 : _a.children;
    if (!htmlStructure)
      return;
    const groupsOrder = Array.from(htmlStructure).map(el => el.getAttribute('data-group-name'));
    const updatedOrderGroupsList = [];
    const fixedGroup = this.getFixedFieldsGroup();
    if (fixedGroup) {
      updatedOrderGroupsList.push(fixedGroup);
    }
    groupsOrder.forEach(groupName => {
      const group = this.groupsListMemory.find(g => g.name === groupName);
      if (group) {
        updatedOrderGroupsList.push(group);
      }
    });
    this.applyGroupUpdate(updatedOrderGroupsList);
  }
  handleFieldsChanged(evt) {
    let updatedGroupsList = [...this.groupsListMemory];
    updatedGroupsList = [...this.handleFieldsOnGroupUpdate(updatedGroupsList, evt, SORTABLE_PATH.FROM)];
    updatedGroupsList = [...this.handleFieldsOnGroupUpdate(updatedGroupsList, evt, SORTABLE_PATH.TO)];
    this.applyGroupUpdate(updatedGroupsList);
  }
  applyGroupUpdate(updatedGroupList) {
    var _a;
    (_a = this.groupsRefList) === null || _a === void 0 ? void 0 : _a.forEach((group) => group === null || group === void 0 ? void 0 : group.cancelEdition());
    if (ObjectUtils.equals(updatedGroupList, this.groupsListMemory)) {
      return false;
    }
    this.groupsListMemory = [...updatedGroupList];
    this.layoutChanged.emit(this.groupsListMemory);
    return true;
  }
  handleFieldsOnGroupUpdate(groupList, evt, path) {
    const element = evt[path];
    const isAvailableFields = element.id === ID_AVAILABLE_FIELDS;
    return isAvailableFields ? groupList : this.updateGroupList(groupList, element, path, evt);
  }
  updateGroupList(groupList, element, path, evt) {
    const groupName = element.getAttribute('data-group-name');
    const updatedGroup = {
      name: groupName,
      fields: getFieldsStructure(evt, path, groupName, this.selectedGuide.name),
    };
    return this.replaceGroup(groupList, updatedGroup);
  }
  replaceGroup(groupList, newGroup) {
    return groupList.map(group => group.name === newGroup.name ? newGroup : group);
  }
  makeGroupsDraggable() {
    try {
      const element = this.getContainerElement(this.ID_GROUPS);
      if (!element)
        return;
      new Sortable(element, {
        group: 'groups',
        ghostClass: 'ghost',
        direction: 'horizontal',
        animation: 15,
        onSort: (evt) => this.handleSortGroups(evt),
      });
    }
    catch (e) {
      console.error(this.getMessage('snkFormConfig.errors.failMakeGroupsDraggable'), this.ID_GROUPS);
    }
  }
  makeFieldContainerDraggable(containerId) {
    try {
      const element = this.getContainerElement(containerId);
      if (!element)
        return;
      new Sortable(element, {
        group: 'fields',
        ghostClass: 'ghost',
        direction: 'vertical',
        animation: 15,
        onAdd: (evt) => this.handleFieldsChanged(evt),
        onRemove: (evt) => this.handleFieldsChanged(evt),
        onSort: (evt) => this.handleFieldsChanged(evt),
      });
    }
    catch (e) {
      console.error(this.getMessage('snkFormConfig.errors.failMakeFieldContainerDraggable'), containerId);
    }
  }
  getContainerElement(id) {
    return this.el.querySelector(`[id="${id}"]`);
  }
  handleSetFieldAsAvailable(field) {
    var _a;
    const groupName = (_a = field.group) !== null && _a !== void 0 ? _a : GROUP_NAMES.noGroup;
    const group = this.groupsListMemory.find(group => group.name === groupName);
    const fields = group.fields.filter(f => f.name !== field.name);
    const updatedGroup = { name: groupName, fields };
    const updatedList = [...this.replaceGroup(this.groupsListMemory, updatedGroup)];
    this.applyGroupUpdate(updatedList);
  }
  removeGroup(group) {
    var _a, _b;
    const groupFields = (_b = (_a = this.groupsListMemory.find(f => f.name === group.name)) === null || _a === void 0 ? void 0 : _a.fields) !== null && _b !== void 0 ? _b : [];
    const updatedGroupList = [...this.groupsListMemory.filter(g => g.name !== group.name)];
    if (this.applyGroupUpdate(updatedGroupList)) {
      this.setFieldListAsAvailable.emit(groupFields);
    }
  }
  handleRenameGroup(editDetails) {
    const { value: actualName, newValue: newGroupName } = editDetails.detail;
    const updatedGroupList = this.groupsListMemory.map(group => {
      if (group.name !== actualName)
        return Object.assign({}, group);
      const updatedFields = group.fields.map(f => {
        return Object.assign(Object.assign({}, f), { group: newGroupName });
      });
      return { name: newGroupName, fields: updatedFields };
    });
    this.applyGroupUpdate(updatedGroupList);
  }
  canSaveGroup(newLabel, index) {
    const hasOtherWithThisName = this.groupsList.some(group => {
      var _a;
      return ((_a = group.name) !== null && _a !== void 0 ? _a : '').toLocaleLowerCase() === (newLabel !== null && newLabel !== void 0 ? newLabel : '').toLocaleLowerCase();
    });
    if (!hasOtherWithThisName)
      return true;
    const title = this.getMessage('snkFormConfig.confirm.title');
    const message = this.buildDuplicatedGroupNameContent(newLabel);
    ApplicationUtils.alert(title, message).then(() => { var _a; return (_a = this.groupsRefList[index]) === null || _a === void 0 ? void 0 : _a.applyFocusTextEdit(); });
    return false;
  }
  buildDuplicatedGroupNameContent(newLabel) {
    const tabName = this.selectedGuide.name;
    return `
        ${this.getMessage('snkFormConfig.alert.titleGroupExists')}
        <b>${newLabel}</b>
        ${this.getMessage('snkFormConfig.alert.inTab')}
        <b>${tabName === TAB_NAMES.main ? this.getMessage('snkFormConfig.form.mainArea') : tabName}</b>.
        <br/><br/>
        ${this.getMessage('snkFormConfig.alert.infoValidTitle')}
      `;
  }
  buildDuplicatedTabNameContent(newLabel) {
    return `
        ${this.getMessage('snkFormConfig.alert.titleGuideExists')}
        <b>${newLabel}</b>.
        <br/><br/>
        ${this.getMessage('snkFormConfig.alert.infoValidTitle')}
      `;
  }
  buildReservedTabNameContent(newLabel) {
    return `
        ${this.getMessage('snkFormConfig.alert.titleGuideReservedName')}
        <br/><br/>
        <b>${newLabel}</b>.
        <br/><br/>
        ${this.getMessage('snkFormConfig.alert.infoValidTitle')}
      `;
  }
  renderFields(fields) {
    if (fields.length === 0)
      return;
    return fields.map(field => (h("field-item", { key: field.name, fieldConfig: field, onLayoutConfig: true, dataUnit: this.dataUnit, messagesBuilder: this.messagesBuilder, onSetFieldAsAvailable: (e) => this.handleSetFieldAsAvailable(e.detail) })));
  }
  renderDraggableGroups() {
    const draggableGroups = this.groupsList.filter(g => g.name !== GROUP_NAMES.noGroup);
    return (draggableGroups.map((group, index) => {
      var _a;
      return (h("div", { key: group.name, class: 'group-config', "data-group-name": group.name }, h("ez-collapsible-box", { ref: ref => this.groupsRefList[index] = ref, label: group.name, headerSize: "large", editable: true, removable: true, "icon-placement": "left", onEzRemove: () => this.removeGroup(group), onEzSaveEditLabel: (event => this.handleRenameGroup(event)), onEzEditLabelMode: ({ detail }) => this.isEditingGroupName.emit(detail), conditionalSave: (newLabel) => this.canSaveGroup(newLabel, index) }, h("div", { id: this.getFieldsContainerId(group.name), class: `group-content ${!((_a = group.fields) === null || _a === void 0 ? void 0 : _a.length) ? 'empty-content' : ''}`, "data-group-name": group.name }, this.renderFields(group.fields)))));
    }));
  }
  renderFixedFields() {
    var _a, _b;
    const noGroupFields = (_b = (_a = this.groupsList.find(g => g.name === GROUP_NAMES.noGroup)) === null || _a === void 0 ? void 0 : _a.fields) !== null && _b !== void 0 ? _b : [];
    return this.renderFields(noGroupFields);
  }
  handleRenameGuide(editDetails) {
    const { value: actualValue, newValue } = editDetails.detail;
    this.isEditGuideNameActive = false;
    if (newValue === actualValue) {
      return;
    }
    const duplicatedName = this.guideNames.includes(newValue.toLowerCase());
    const forbiddenName = this.reservedGuideNames.includes(newValue.toLowerCase());
    if (forbiddenName || duplicatedName) {
      const title = this.getMessage('snkFormConfig.confirm.title');
      const message = forbiddenName ? this.buildReservedTabNameContent(newValue) : this.buildDuplicatedTabNameContent(newValue);
      ApplicationUtils.alert(title, message).then(() => this._refTextEdit.applyFocusSelect());
      return;
    }
    this.guideRenamed.emit(newValue);
  }
  cancelEditGuideName() {
    this.isEditGuideNameActive = false;
  }
  async startGuideNameEdition() {
    this.isEditGuideNameActive = true;
    setTimeout(async () => {
      await this._refTextEdit.applyFocusSelect();
    }, 200);
  }
  getTextEditStyle() {
    return {
      fontSize: '20px',
      fontWeight: '500',
      fontFamily: 'Roboto',
    };
  }
  renderGuideName() {
    var _a, _b;
    if (this.isEditGuideNameActive) {
      return (h("div", { class: 'guide-header' }, h("ez-text-edit", { class: "collapsible-box__text-edit", ref: ref => this._refTextEdit = ref, value: (_a = this.selectedGuide) === null || _a === void 0 ? void 0 : _a.label, styled: this.getTextEditStyle(), onSaveEdition: (ev) => this.handleRenameGuide(ev), onCancelEdition: () => this.cancelEditGuideName() })));
    }
    return (h("div", { class: 'guide-header' }, h("span", { class: 'guide-header-label' }, (_b = this.selectedGuide) === null || _b === void 0 ? void 0 : _b.label), this.renderEditGuideNameButton()));
  }
  renderEditGuideNameButton() {
    var _a;
    if (((_a = this.selectedGuide) === null || _a === void 0 ? void 0 : _a.name) === TAB_NAMES.main)
      return;
    return (h("ez-button", { mode: "icon", class: "ez-button--tertiary", onClick: () => this.startGuideNameEdition(), size: "medium", iconName: 'edit', title: this.getMessage('snkFormConfig.buttons.rename') }));
  }
  setCustomCSSProperties() {
    document.documentElement.style.setProperty('--empty-content-message', `"${this.getMessage('snkFormConfig.form.labelDropField')}"`);
    document.documentElement.style.setProperty('--no-group-message', `"${this.getMessage('snkFormConfig.form.subTitleInfo')}"`);
  }
  componentDidRender() {
    this.makeGroupsDraggable();
    this.makeDynamicFieldsContainersDraggable();
    this.makeFieldContainerDraggable(GROUP_NAMES.noGroup);
    this.setCustomCSSProperties();
  }
  render() {
    if (!this.selectedGuide) {
      return (h(Host, { class: 'ez-flex-item--auto ez-size-height--full ez-box ez-padding--medium content-container' }, h("span", { class: 'no-guide-selected' }, this.getMessage('snkFormConfig.form.noGuideSelected'))));
    }
    if (this.selectedGuide.isCustom || this.selectedGuide.isDetail) {
      return (h(Host, { class: 'ez-flex-item--auto ez-size-height--full ez-box ez-padding--medium content-container' }, h("div", { class: 'message-container' }, h("h1", { class: 'no-guide-selected' }, this.getMessage('snkFormConfig.form.canNotEditCustomGuide')), h("span", { class: 'message-config-detail' }, this.getMessage('snkFormConfig.form.canNotEditDetailGuide')))));
    }
    return (h(Host, { class: 'ez-flex-item--auto ez-size-height--full ez-box ez-padding--medium content-container' }, this.renderGuideName(), h("div", { class: 'layout-container' }, h("ez-scroller", { direction: 'vertical', activeShadow: true }, h("div", { class: 'no-group-container' }, h("div", { id: GROUP_NAMES.noGroup, class: 'group-content', "data-group-name": GROUP_NAMES.noGroup }, this.renderFixedFields())), h("hr", null), h("div", { id: this.ID_GROUPS, class: 'groups-container' }, this.renderDraggableGroups()))), h("div", { class: 'add-group-container', onClick: this.handleAddGroup.bind(this) }, h("ez-icon", { slot: "leftIcon", iconName: "plus" }), h("span", null, this.getMessage('snkFormConfig.form.createNewGroup')))));
  }
  get el() { return this; }
  static get watchers() { return {
    "messagesBuilder": ["onMessagesBuilderChange"],
    "selectedGuide": ["observeSelectedGuide"],
    "groupsList": ["observeGroupList"],
    "isEditGuideNameActive": ["observeIsEditGuideNameActive"]
  }; }
  static get style() { return fieldsLayoutCss; }
}, [2, "fields-layout", {
    "selectedGuide": [16],
    "groupsList": [16],
    "guideNames": [16],
    "dataUnit": [16],
    "messagesBuilder": [1040],
    "isEditGuideNameActive": [32],
    "addFieldToLayout": [64]
  }, [[16, "fieldConfigChanged", "handleFieldConfigChanged"]]]);
function defineCustomElement() {
  if (typeof customElements === "undefined") {
    return;
  }
  const components = ["fields-layout", "field-config", "field-item"];
  components.forEach(tagName => { switch (tagName) {
    case "fields-layout":
      if (!customElements.get(tagName)) {
        customElements.define(tagName, FieldLayoutComponent);
      }
      break;
    case "field-config":
      if (!customElements.get(tagName)) {
        defineCustomElement$2();
      }
      break;
    case "field-item":
      if (!customElements.get(tagName)) {
        defineCustomElement$1();
      }
      break;
  } });
}

export { FieldLayoutComponent as F, defineCustomElement as d };
