import { h, Host } from '@stencil/core';
import { ActionDefaultFilter } from '../../../types/default-filters';
export class SnkDefaultFilter {
  constructor() {
    this.OFFSET_HEIGHT = 5;
    this.getMessage = undefined;
    this.hasDefaultFilter = false;
    this._opened = false;
  }
  repositionDropDown() {
    if (!this._refIcon || !this._refDropdown) {
      return;
    }
    const boundingIcon = this._refIcon.getBoundingClientRect();
    const boundingDropDown = this._refDropdown.getBoundingClientRect();
    this._refDropdown.style.top = (boundingIcon.y + boundingIcon.height + this.OFFSET_HEIGHT) + "px";
    this._refDropdown.style.left = ((boundingIcon.x + (boundingIcon.width / 2)) - boundingDropDown.width) + "px";
  }
  toggleDropdown() {
    this._opened = !this._opened;
  }
  closeDropdown(evt) {
    const target = evt === null || evt === void 0 ? void 0 : evt.target;
    if (!target) {
      return;
    }
    if (!target.closest("#dropdown-parent-demo")) {
      this._opened = false;
    }
  }
  buildItems() {
    return ([
      {
        id: ActionDefaultFilter.EDIT,
        label: this.getMessage('defaultFilter.edit'),
      },
      {
        id: ActionDefaultFilter.REMOVE,
        label: this.getMessage('defaultFilter.remove'),
      },
    ]);
  }
  onValueSelected({ detail: item }) {
    this._opened = false;
    this.actionSelected.emit(item.id);
  }
  openCreateNewFilter() {
    this.actionSelected.emit(ActionDefaultFilter.CREATE);
  }
  componentDidRender() {
    this.repositionDropDown();
  }
  componentWillLoad() {
    this._items = this.buildItems();
    document.removeEventListener("scroll", this.repositionDropDown.bind(this));
    document.addEventListener("scroll", this.repositionDropDown.bind(this));
  }
  render() {
    if (!this.hasDefaultFilter) {
      return (h(Host, null, h("button", { class: "default_container new_filter", onClick: this.openCreateNewFilter.bind(this) }, h("span", null, this.getMessage('defaultFilter.create')))));
    }
    return (h(Host, null, h("button", { class: "default_container", onClick: this.toggleDropdown.bind(this) }, h("span", null, this.getMessage('defaultFilter.title')), h("ez-icon", { ref: (ref) => this._refIcon = ref, iconName: 'dots-vertical' })), this._opened &&
      h("ez-dropdown", { ref: (ref) => this._refDropdown = ref, items: this._items, onEzClick: this.onValueSelected.bind(this), onEzOutsideClick: this.closeDropdown.bind(this) })));
  }
  static get is() { return "snk-default-filter"; }
  static get encapsulation() { return "scoped"; }
  static get originalStyleUrls() {
    return {
      "$": ["snk-default-filter.css"]
    };
  }
  static get styleUrls() {
    return {
      "$": ["snk-default-filter.css"]
    };
  }
  static get properties() {
    return {
      "getMessage": {
        "type": "unknown",
        "mutable": false,
        "complexType": {
          "original": "(key: string, props?: any) => string",
          "resolved": "(key: string, props?: any) => string",
          "references": {}
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": "Usado pra flexibilizar os verbetes e mensagens de acordo com o SnkMessageBuilder."
        }
      },
      "hasDefaultFilter": {
        "type": "boolean",
        "mutable": false,
        "complexType": {
          "original": "boolean",
          "resolved": "boolean",
          "references": {}
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": "Informa se j\u00E1 existe um filtro padr\u00E3o criado."
        },
        "attribute": "has-default-filter",
        "reflect": false,
        "defaultValue": "false"
      }
    };
  }
  static get states() {
    return {
      "_opened": {}
    };
  }
  static get events() {
    return [{
        "method": "actionSelected",
        "name": "actionSelected",
        "bubbles": true,
        "cancelable": true,
        "composed": true,
        "docs": {
          "tags": [],
          "text": ""
        },
        "complexType": {
          "original": "ActionDefaultFilter",
          "resolved": "ActionDefaultFilter.CREATE | ActionDefaultFilter.EDIT | ActionDefaultFilter.REMOVE",
          "references": {
            "ActionDefaultFilter": {
              "location": "import",
              "path": "../../../types/default-filters"
            }
          }
        }
      }];
  }
}
