import { h, Host } from '@stencil/core';
import { ApplicationUtils } from '@sankhyalabs/ezui/dist/collection/utils';
export class ConfigHeader {
  constructor() {
    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()));
  }
  static get is() { return "config-header"; }
  static get encapsulation() { return "scoped"; }
  static get originalStyleUrls() {
    return {
      "$": ["config-header.css"]
    };
  }
  static get styleUrls() {
    return {
      "$": ["config-header.css"]
    };
  }
  static get properties() {
    return {
      "configOptions": {
        "type": "unknown",
        "mutable": false,
        "complexType": {
          "original": "Array<IUserConfig>",
          "resolved": "IUserConfig[]",
          "references": {
            "Array": {
              "location": "global"
            },
            "IUserConfig": {
              "location": "import",
              "path": "../../../lib/http/data-fetcher/fetchers/form-config-fetcher"
            }
          }
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": ""
        },
        "defaultValue": "[]"
      },
      "selectedConfig": {
        "type": "unknown",
        "mutable": false,
        "complexType": {
          "original": "IUserConfig",
          "resolved": "IUserConfig",
          "references": {
            "IUserConfig": {
              "location": "import",
              "path": "../../../lib/http/data-fetcher/fetchers/form-config-fetcher"
            }
          }
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": ""
        }
      },
      "messagesBuilder": {
        "type": "unknown",
        "mutable": true,
        "complexType": {
          "original": "SnkMessageBuilder",
          "resolved": "SnkMessageBuilder",
          "references": {
            "SnkMessageBuilder": {
              "location": "import",
              "path": "../../../lib/message/SnkMessageBuilder"
            }
          }
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": ""
        }
      },
      "hasChanges": {
        "type": "boolean",
        "mutable": false,
        "complexType": {
          "original": "boolean",
          "resolved": "boolean",
          "references": {}
        },
        "required": false,
        "optional": true,
        "docs": {
          "tags": [],
          "text": ""
        },
        "attribute": "has-changes",
        "reflect": false,
        "defaultValue": "false"
      },
      "optionConfigChanged": {
        "type": "boolean",
        "mutable": false,
        "complexType": {
          "original": "boolean",
          "resolved": "boolean",
          "references": {}
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": ""
        },
        "attribute": "option-config-changed",
        "reflect": false,
        "defaultValue": "false"
      }
    };
  }
  static get states() {
    return {
      "isEditingGuide": {},
      "isEditingGroup": {}
    };
  }
  static get events() {
    return [{
        "method": "configClose",
        "name": "configClose",
        "bubbles": true,
        "cancelable": true,
        "composed": true,
        "docs": {
          "tags": [],
          "text": ""
        },
        "complexType": {
          "original": "void",
          "resolved": "void",
          "references": {}
        }
      }, {
        "method": "saveConfig",
        "name": "saveConfig",
        "bubbles": true,
        "cancelable": true,
        "composed": true,
        "docs": {
          "tags": [],
          "text": ""
        },
        "complexType": {
          "original": "void",
          "resolved": "void",
          "references": {}
        }
      }];
  }
  static get listeners() {
    return [{
        "name": "isEditingGuideName",
        "method": "handleIsEditingGuideName",
        "target": "body",
        "capture": false,
        "passive": false
      }, {
        "name": "isEditingGroupName",
        "method": "handleIsEditingGroupName",
        "target": "body",
        "capture": false,
        "passive": false
      }];
  }
}
