import { ElementIDUtils } from '@sankhyalabs/core';
import { h, Host } from '@stencil/core';
import FilterItemType from '../filter-item-type.enum';
import { EPresentationMode } from './enum/presentationMode';
export class SnkFilterBinarySelect {
  constructor() {
    this._noOptionValue = 'NO_VALUE';
    this._radioButtonValue = undefined;
    this.value = undefined;
    this.config = undefined;
    this.presentationMode = EPresentationMode.CHIP;
    this.errorMessage = undefined;
  }
  isSelected(option) {
    return this.value == undefined || this.value === option;
  }
  /**
   * Emitido quando acontece a alteração de valor do componente snk-filter-binary-select
   */
  ezChangeListener(evt) {
    this.errorMessage = undefined;
    if (this.presentationMode === EPresentationMode.MODAL) {
      if (!evt.detail) {
        this.assertCheckedOption(evt.target);
      }
      this.updateValue();
      return;
    }
    this.value = evt.detail === this._noOptionValue ? undefined : evt.detail;
    this._radioButtonValue = evt.detail;
  }
  /**
   * Redefine os valores padrão do componente.
   */
  async resetValues() {
    this.ezChangeListener({ detail: this._noOptionValue });
  }
  /**
   * Limpa o valor do componente restaurando o valor original da configuração.
   */
  async clearValue() {
    this.value = this.config.value;
    this._radioButtonValue = this.value || this._noOptionValue;
  }
  /**
   * Define o foco no componente snk-filter-binary-select.
   */
  async setFocus() {
    if (this.presentationMode === EPresentationMode.MODAL) {
      this._checkOne.setFocus();
      return;
    }
    this._element.querySelector('ez-radio-button').focus();
  }
  assertCheckedOption(updatingCheck) {
    if (updatingCheck === this._checkOne) {
      this._checkTwo.value = true;
    }
    else {
      this._checkOne.value = true;
    }
  }
  updateValue() {
    if (!this._checkOne.value || !this._checkTwo.value) {
      const [optOne, optTwo] = this.config.props.options;
      this.value = this._checkOne.value ? optOne.name : optTwo.name;
    }
    else {
      this.value = undefined;
    }
    this.valueChanged.emit(this.value);
  }
  setRadioValue() {
    this._radioButtonValue = this.value || this._noOptionValue;
  }
  componentDidLoad() {
    if (this._element) {
      ElementIDUtils.addIDInfo(this._element, 'filterContentEditor');
    }
  }
  componentWillLoad() {
    this.setRadioValue();
  }
  componentShouldUpdate() {
    this.setRadioValue();
  }
  disconnectedCallback() {
    this.value = this.config.value;
  }
  render() {
    if (!this.config || this.config.type !== FilterItemType.BINARY_SELECT) {
      return undefined;
    }
    const [optOne, optTwo] = this.config.props.options;
    if (this.presentationMode === EPresentationMode.MODAL) {
      return (h(Host, null, h("div", { class: "ez-row ez-size-height--full" }, h("label", { class: "ez-text ez-title--small ez-title--primary ez-size-height--full ez-flex ez-flex--row ez-flex--align-items-center ez-col ez-col--sd-4" }, this.config.detailModal), h("div", { class: "ez-col ez-col--sd-4" }, h("ez-check", { ref: (ref) => this._checkOne = ref, id: `${this.config.id}_${optOne.name || "option1"}`, class: "sc-snk-filter-bar editor__ez-check", label: optOne.label, enabled: this.config.enabled, value: this.isSelected(optOne.name) })), h("div", { class: "ez-col ez-col--sd-4" }, h("ez-check", { ref: (ref) => this._checkTwo = ref, id: `${this.config.id}_${optTwo.name || "option2"}`, class: "sc-snk-filter-bar editor__ez-check", label: optTwo.label, enabled: this.config.enabled, value: this.isSelected(optTwo.name) })), this.errorMessage &&
        h("span", { class: 'ez-text ez-text--small ez-text--error ez-margin--small' }, this.errorMessage))));
    }
    return (h(Host, null, h("ez-tooltip", { active: this.config.enabled === false, message: this.config.disabledMessage, type: 'warning' }, h("ez-radio-button", { value: this._radioButtonValue }, h("ez-radio-button-option", { id: `${this.config.id}_${optOne.name || "option1"}`, class: "sc-snk-filter-bar editor__ez-check", label: optOne.label, value: optOne.name }), h("ez-radio-button-option", { id: `${this.config.id}_${optTwo.name || "option2"}`, class: "sc-snk-filter-bar editor__ez-check", label: optTwo.label, value: optTwo.name }), h("ez-radio-button-option", { id: `${this.config.id}_option3`, class: "sc-snk-filter-bar editor__ez-check", label: `${optOne.label} + ${optTwo.label}`, value: this._noOptionValue })), this.errorMessage &&
      h("span", { class: 'ez-text ez-text--small ez-text--error ez-margin--small' }, this.errorMessage))));
  }
  static get is() { return "snk-filter-binary-select"; }
  static get properties() {
    return {
      "value": {
        "type": "any",
        "mutable": true,
        "complexType": {
          "original": "any",
          "resolved": "any",
          "references": {}
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": "Define o valor do componente snk-filter-binary-select"
        },
        "attribute": "value",
        "reflect": true
      },
      "config": {
        "type": "unknown",
        "mutable": false,
        "complexType": {
          "original": "SnkFilterItemConfig",
          "resolved": "SnkFilterItemConfig",
          "references": {
            "SnkFilterItemConfig": {
              "location": "import",
              "path": "../snk-filter-item"
            }
          }
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": "Define as configura\u00E7\u00F5es do componente snk-filter-binary-select"
        }
      },
      "presentationMode": {
        "type": "number",
        "mutable": false,
        "complexType": {
          "original": "EPresentationMode",
          "resolved": "EPresentationMode.CHIP | EPresentationMode.MODAL",
          "references": {
            "EPresentationMode": {
              "location": "import",
              "path": "./enum/presentationMode"
            }
          }
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": "Define a varia\u00E7\u00E3o do componente"
        },
        "attribute": "presentation-mode",
        "reflect": false,
        "defaultValue": "EPresentationMode.CHIP"
      },
      "errorMessage": {
        "type": "string",
        "mutable": true,
        "complexType": {
          "original": "string",
          "resolved": "string",
          "references": {}
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": "Usado para exibir mensagens de erro."
        },
        "attribute": "error-message",
        "reflect": true
      }
    };
  }
  static get events() {
    return [{
        "method": "valueChanged",
        "name": "valueChanged",
        "bubbles": true,
        "cancelable": true,
        "composed": true,
        "docs": {
          "tags": [],
          "text": "Emitido quando o valor do filtro \u00E9 alterado."
        },
        "complexType": {
          "original": "any",
          "resolved": "any",
          "references": {}
        }
      }];
  }
  static get methods() {
    return {
      "resetValues": {
        "complexType": {
          "signature": "() => Promise<void>",
          "parameters": [],
          "references": {
            "Promise": {
              "location": "global"
            },
            "CustomEvent": {
              "location": "global"
            }
          },
          "return": "Promise<void>"
        },
        "docs": {
          "text": "Redefine os valores padr\u00E3o do componente.",
          "tags": []
        }
      },
      "clearValue": {
        "complexType": {
          "signature": "() => Promise<void>",
          "parameters": [],
          "references": {
            "Promise": {
              "location": "global"
            }
          },
          "return": "Promise<void>"
        },
        "docs": {
          "text": "Limpa o valor do componente restaurando o valor original da configura\u00E7\u00E3o.",
          "tags": []
        }
      },
      "setFocus": {
        "complexType": {
          "signature": "() => Promise<void>",
          "parameters": [],
          "references": {
            "Promise": {
              "location": "global"
            }
          },
          "return": "Promise<void>"
        },
        "docs": {
          "text": "Define o foco no componente snk-filter-binary-select.",
          "tags": []
        }
      }
    };
  }
  static get elementRef() { return "_element"; }
  static get listeners() {
    return [{
        "name": "ezChange",
        "method": "ezChangeListener",
        "target": undefined,
        "capture": false,
        "passive": false
      }];
  }
}
