import { h, Host } from '@stencil/core';
import { ObjectUtils } from '@sankhyalabs/core';
import { ApplicationUtils } from '@sankhyalabs/ezui/dist/collection/utils';
export class ConfigsButton {
  constructor() {
    this.configOptions = [];
    this.selectedConfig = undefined;
    this.hasChanges = false;
    this.messagesBuilder = undefined;
  }
  getMessage(key, params) {
    return this.messagesBuilder.getMessage(key, params);
  }
  async showShareConfigOptions() {
    if (this._refOptionsPopover && this._refShareConfigBtn) {
      await this._refOptionsPopover.showUnder(this._refShareConfigBtn);
    }
  }
  async handleSelectOption(option) {
    if (ObjectUtils.equals(option, this.selectedConfig))
      return;
    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.selectedConfig = option;
    this.formConfigOptionSelected.emit(option);
    await this._refOptionsPopover.hide();
  }
  getButtonLabel() {
    var _a, _b;
    return (_b = (_a = this.selectedConfig) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : this.getMessage('snkFormConfig.configs.buttonLabel');
  }
  renderConfigOptions() {
    return this.configOptions.map((option) => {
      return (h("div", { class: `main-item`, onClick: async () => await this.handleSelectOption(option) }, h("span", { class: 'item-label' }, " ", option.name), this.renderCheckIcon(option)));
    });
  }
  renderCheckIcon(option) {
    if (this.selectedConfig && this.selectedConfig.origin === option.origin)
      return h("ez-icon", { iconName: "check" });
  }
  render() {
    return (h(Host, null, h("ez-button", { ref: ref => this._refShareConfigBtn = ref, label: this.getButtonLabel(), onClick: async () => await this.showShareConfigOptions() }, h("ez-icon", { slot: "rightIcon", iconName: "chevron-down" })), h("ez-popover-plus", { ref: ref => this._refOptionsPopover = ref, autoClose: true, overlayType: "none" }, h("div", { class: 'dropdown-container' }, this.renderConfigOptions()))));
  }
  static get is() { return "configs-button"; }
  static get encapsulation() { return "scoped"; }
  static get originalStyleUrls() {
    return {
      "$": ["configs-button.css"]
    };
  }
  static get styleUrls() {
    return {
      "$": ["configs-button.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": ""
        }
      },
      "hasChanges": {
        "type": "boolean",
        "mutable": false,
        "complexType": {
          "original": "boolean",
          "resolved": "boolean",
          "references": {}
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": ""
        },
        "attribute": "has-changes",
        "reflect": false,
        "defaultValue": "false"
      },
      "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": ""
        }
      }
    };
  }
  static get events() {
    return [{
        "method": "formConfigOptionSelected",
        "name": "formConfigOptionSelected",
        "bubbles": true,
        "cancelable": true,
        "composed": true,
        "docs": {
          "tags": [],
          "text": ""
        },
        "complexType": {
          "original": "IUserConfig",
          "resolved": "IUserConfig",
          "references": {
            "IUserConfig": {
              "location": "import",
              "path": "../../../../lib/http/data-fetcher/fetchers/form-config-fetcher"
            }
          }
        }
      }];
  }
}
