import { ElementIDUtils } from '@sankhyalabs/core';
import { h } from '@stencil/core';
import FilterItemType from '../filter-item-type.enum';
export class SnkFilterCheckboxList {
  constructor() {
    this.config = undefined;
    this.errorMessage = undefined;
    this.optionsList = [];
  }
  /**
   * Define o foco no componente snk-filter-checkbox-list.
   */
  async setFocus() {
    var _a, _b;
    (_b = (_a = this._element) === null || _a === void 0 ? void 0 : _a.querySelector('ez-check')) === null || _b === void 0 ? void 0 : _b.setFocus();
  }
  /**
   * Limpa o valor do componente restaurando o valor original da configuração.
   */
  async clearValue() {
    var _a;
    this.config = Object.assign(Object.assign({}, this.config), { value: this.config.value });
    this.valueChanged.emit((_a = this.config) === null || _a === void 0 ? void 0 : _a.value);
  }
  componentDidLoad() {
    if (this._element) {
      ElementIDUtils.addIDInfo(this._element, 'filterContentEditor');
    }
  }
  handleToggleCheck(itemChanged, checked) {
    var _a, _b;
    this.errorMessage = undefined;
    let configValueMap = (_a = this.config.value) !== null && _a !== void 0 ? _a : {};
    configValueMap[itemChanged.name] = checked;
    this.config = Object.assign(Object.assign({}, this.config), { value: configValueMap });
    this.valueChanged.emit((_b = this.config) === null || _b === void 0 ? void 0 : _b.value);
  }
  renderOptions() {
    return this.optionsList.map(option => (h("ez-check", { label: option.label, onEzChange: (event) => this.handleToggleCheck(option, event.detail), value: !!this.isChecked(option.name), enabled: this.config.enabled })));
  }
  isChecked(itemName) {
    var _a;
    return (_a = this.config.value) === null || _a === void 0 ? void 0 : _a[itemName];
  }
  buidOptions() {
    var _a, _b, _c;
    const propsOptions = (_c = (_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.props) === null || _b === void 0 ? void 0 : _b.options) !== null && _c !== void 0 ? _c : [];
    this.optionsList = [...propsOptions];
  }
  componentWillRender() {
    this.buidOptions();
  }
  render() {
    if (!this.config || this.config.type !== FilterItemType.CHECK_BOX_LIST) {
      return undefined;
    }
    return (h("ez-tooltip", { active: this.config.enabled === false, message: this.config.disabledMessage, type: 'warning' }, h("div", { class: 'ez-flex ez-flex--column' }, this.renderOptions(), this.errorMessage &&
      h("span", { class: 'ez-text ez-text--small ez-text--error ez-margin--small' }, this.errorMessage))));
  }
  static get is() { return "snk-filter-checkbox-list"; }
  static get properties() {
    return {
      "config": {
        "type": "unknown",
        "mutable": true,
        "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-multi-select"
        }
      },
      "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 states() {
    return {
      "optionsList": {}
    };
  }
  static get events() {
    return [{
        "method": "valueChanged",
        "name": "valueChanged",
        "bubbles": true,
        "cancelable": true,
        "composed": true,
        "docs": {
          "tags": [],
          "text": "Emite um evento customizado ao realizar altera\u00E7\u00E3o nos valores do componente"
        },
        "complexType": {
          "original": "CustomEvent",
          "resolved": "CustomEvent<any>",
          "references": {
            "CustomEvent": {
              "location": "global"
            }
          }
        }
      }];
  }
  static get methods() {
    return {
      "setFocus": {
        "complexType": {
          "signature": "() => Promise<void>",
          "parameters": [],
          "references": {
            "Promise": {
              "location": "global"
            }
          },
          "return": "Promise<void>"
        },
        "docs": {
          "text": "Define o foco no componente snk-filter-checkbox-list.",
          "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": []
        }
      }
    };
  }
  static get elementRef() { return "_element"; }
}
