import { r as registerInstance, e as createEvent, h, f as Host } from './index-e9c7ceaa.js';
import { ApplicationUtils } from '@sankhyalabs/ezui/dist/collection/utils';

const configHeaderCss = ".sc-config-header-h{display:flex;padding-top:12px;padding-left:12px;align-items:center;width:100%;justify-content:space-between}.left-group.sc-config-header{display:flex;gap:8px}.right-group.sc-config-header{padding-right:18px;display:flex}";

const ConfigHeader = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.configClose = createEvent(this, "configClose", 7);
    this.saveConfig = createEvent(this, "saveConfig", 7);
    this.isEditingGuide = false;
    this.isEditingGroup = false;
    this.configOptions = [];
    this.selectedConfig = undefined;
    this.messagesBuilder = undefined;
    this.hasChanges = false;
    this.optionConfigChanged = false;
  }
  handleIsEditingGuideName({ detail }) {
    this.isEditingGuide = detail;
  }
  handleIsEditingGroupName({ detail }) {
    this.isEditingGroup = detail;
  }
  saveFormConfig() {
    if (this.isEditingGuide || this.isEditingGroup) {
      const title = this.getMessage('snkFormConfig.confirm.title');
      const message = this.isEditingGuide
        ? this.getMessage('snkFormConfig.alert.titleGuideEditing')
        : this.getMessage('snkFormConfig.alert.titleGroupEditing');
      ApplicationUtils.alert(title, message);
      return;
    }
    this.saveConfig.emit();
  }
  async closeFormConfig() {
    if (this.hasChanges) {
      const title = this.getMessage('snkFormConfig.confirm.title');
      const msg = this.getMessage('snkFormConfig.confirm.cancel');
      const confirm = await ApplicationUtils.confirm(title, msg);
      if (!confirm)
        return;
    }
    this.configClose.emit();
  }
  getMessage(key, params) {
    return this.messagesBuilder.getMessage(key, params);
  }
  openConfirmDialog(msg = '', title = this.getMessage('snkFormConfig.confirm.title')) {
    return ApplicationUtils.confirm(title, msg);
  }
  async applyOptionConfig() {
    var _a, _b;
    const name = (_b = (_a = this.selectedConfig) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : this.getMessage('snkFormConfig.configs.selectedConfig');
    const msg = this.getMessage('snkFormConfig.confirm.apply').replace('{0}', name);
    const confirm = await this.openConfirmDialog(msg);
    if (confirm) {
      this.saveConfig.emit();
    }
  }
  renderSaveActions() {
    if (!this.hasChanges && !this.optionConfigChanged)
      return;
    if (this.hasChanges) {
      return (h("div", { class: "right-group" }, h("ez-button", { title: this.getMessage('snkFormConfig.actions.cancel'), label: this.getMessage('snkFormConfig.actions.cancel'), onClick: this.closeFormConfig.bind(this), size: "medium" }), h("ez-button", { title: this.getMessage('snkFormConfig.actions.save'), label: this.getMessage('snkFormConfig.actions.save'), class: 'ez-button--primary ez-padding-left--medium', onClick: () => this.saveFormConfig(), size: "medium" })));
    }
    return (h("div", { class: "right-group" }, h("ez-button", { title: this.getMessage('snkFormConfig.actions.cancel'), label: this.getMessage('snkFormConfig.actions.cancel'), onClick: this.closeFormConfig.bind(this), size: "medium" }), h("ez-button", { title: this.getMessage('snkFormConfig.actions.save'), label: this.getMessage('snkFormConfig.applyConfig'), class: 'ez-button--primary ez-padding-left--medium', onClick: async () => await this.applyOptionConfig(), size: "medium" })));
  }
  render() {
    return (h(Host, null, h("div", { class: 'left-group' }, h("ez-button", { mode: "icon", title: this.getMessage('snkFormConfig.goBackTitle'), iconName: "arrow_back", class: "ez-padding--small", size: "small", onClick: this.closeFormConfig.bind(this), id: "formConfigToBack" }), h("h1", { class: "ez-title ez-title--primary ez-title--extra-large ez-padding--small" }, this.getMessage('snkFormConfig.title')), h("configs-button", { hasChanges: this.hasChanges, configOptions: this.configOptions, selectedConfig: this.selectedConfig, messagesBuilder: this.messagesBuilder })), this.renderSaveActions()));
  }
};
ConfigHeader.style = configHeaderCss;

export { ConfigHeader as config_header };
