import { h } from '@stencil/core';
import { FormConfigFetcher } from '../../lib';
import { ApplicationContext } from '@sankhyalabs/core';
import { FormLayout } from '@sankhyalabs/ezui/dist/collection/utils';
const FORM_COL_CONFIG_PREFIX = 'formColCfg.';
export class SnkLayoutFormConfig {
  constructor() {
    this._formConfigFetcher = new FormConfigFetcher();
    this.messagesBuilder = undefined;
    this.layoutType = FormLayout.SIDE_BY_SIDE;
  }
  /**
   * Salva o tipo de layout escolhido. Sem retorno.
   */
  async save() {
    this._LayoutFormConfigSingleton.setConfig(this.layoutType);
    this._formConfigFetcher.saveLayoutFormConfig(FORM_COL_CONFIG_PREFIX + this._resourceID, { config: this.layoutType, newConfig: true });
  }
  async componentWillLoad() {
    const _application = ApplicationContext.getContextValue("__SNK__APPLICATION__");
    this._LayoutFormConfigSingleton = await (_application === null || _application === void 0 ? void 0 : _application.getLayoutFormConfig());
    this._resourceID = await (_application === null || _application === void 0 ? void 0 : _application.getResourceID());
    this.setLayoutInit();
  }
  setLayoutInit() {
    if (!this._LayoutFormConfigSingleton)
      return;
    const { config } = this._LayoutFormConfigSingleton;
    if (config == null) {
      this.layoutType = FormLayout.SIDE_BY_SIDE;
      return;
    }
    const configAsNumber = Number(config);
    this.layoutType = Object.values(FormLayout).includes(configAsNumber)
      ? configAsNumber
      : FormLayout.SIDE_BY_SIDE;
  }
  toggleLayout(key) {
    this.layoutType = key;
  }
  getMessage(key, params) {
    var _a;
    return (_a = this.messagesBuilder) === null || _a === void 0 ? void 0 : _a.getMessage(key, params);
  }
  render() {
    return (h("div", { class: "ez-flex ez-flex--column" }, h("div", { class: "ez-margin-bottom--small" }, h("ez-check", { label: this.getMessage('snkLayoutFormConfig.cascadeLayout.label'), class: "ez-margin-bottom--small", onEzChange: () => this.toggleLayout(FormLayout.CASCADE), value: this.layoutType === FormLayout.CASCADE }), h("snk-view-representation", { mode: FormLayout.CASCADE }), h("label", { class: "ez-label ez-margin--small ez-flex ez-text--medium" }, this.getMessage('snkLayoutFormConfig.cascadeLayout.description'))), h("div", null, h("ez-check", { label: this.getMessage('snkLayoutFormConfig.sideBySideLayout.label'), class: "ez-margin-bottom--small", onEzChange: () => this.toggleLayout(FormLayout.SIDE_BY_SIDE), value: this.layoutType === FormLayout.SIDE_BY_SIDE }), h("snk-view-representation", { mode: FormLayout.SIDE_BY_SIDE }), h("label", { class: "ez-label ez-margin--small ez-flex ez-text--medium" }, this.getMessage('snkLayoutFormConfig.sideBySideLayout.description')))));
  }
  static get is() { return "snk-layout-form-config"; }
  static get encapsulation() { return "scoped"; }
  static get originalStyleUrls() {
    return {
      "$": ["snk-layout-form-config.css"]
    };
  }
  static get styleUrls() {
    return {
      "$": ["snk-layout-form-config.css"]
    };
  }
  static get properties() {
    return {
      "messagesBuilder": {
        "type": "unknown",
        "mutable": false,
        "complexType": {
          "original": "SnkMessageBuilder",
          "resolved": "SnkMessageBuilder",
          "references": {
            "SnkMessageBuilder": {
              "location": "import",
              "path": "../../lib/message/SnkMessageBuilder"
            }
          }
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": ""
        }
      }
    };
  }
  static get states() {
    return {
      "layoutType": {}
    };
  }
  static get methods() {
    return {
      "save": {
        "complexType": {
          "signature": "() => Promise<void>",
          "parameters": [],
          "references": {
            "Promise": {
              "location": "global"
            }
          },
          "return": "Promise<void>"
        },
        "docs": {
          "text": "Salva o tipo de layout escolhido. Sem retorno.",
          "tags": []
        }
      }
    };
  }
}
