'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-a7b0c73d.js');
const core = require('@sankhyalabs/core');

const ezViewStackCss = ":host{width:100%;height:100%}.ez-view-stack__container{width:100%;height:100%}";

const EzViewStack = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    /**
     * Indetificador único do componente
     */
    this._dataKey = '';
  }
  /**
  * Exibe a visão de acordo com o índice.
  */
  async show(index) {
    if (index !== this._focusedIndex && 0 <= index && index < this._totalStackItems) {
      this.policyHideRemove();
      this.policyShowAppend(index);
      this._focusedIndex = index;
    }
  }
  /**
  * Obtem o índice selecionado.
  */
  async getSelectedIndex() {
    return this._focusedIndex;
  }
  policyHideRemove() {
    let element = this._hostElem.querySelector(`#${this._dataKey}_el_${this._focusedIndex}`);
    this.checkElementVisibility(element);
  }
  policyShowAppend(index) {
    let selectedElement = this._hostElem.querySelector(`#${this._dataKey}_el_${index}`);
    if (selectedElement) {
      selectedElement.setAttribute("style", "visibility: visible");
    }
    else {
      this._hostElem.append(this._elemList[index]);
    }
  }
  checkElementVisibility(element) {
    if (element.getAttribute("hidePolicy") === "hide" || !element.getAttribute("hidePolicy")) {
      element.setAttribute("style", "display: none");
    }
    else if (element.getAttribute("hidePolicy") === "remove") {
      element.remove();
    }
  }
  componentWillRender() {
    this._dataKey = 'id_'.concat(core.JSUtils.generateUUID());
    if (!this._elemList) {
      this._elemList = [];
      this._focusedIndex = 0;
      this._totalStackItems = 0;
      this._hostElem.classList.add("ez-view-stack__container");
      this._hostElem.querySelectorAll(":scope > stack-item").forEach((element, index) => {
        element.setAttribute("id", `${this._dataKey}_el_${index}`);
        if (index !== 0) {
          this.checkElementVisibility(element);
        }
        else {
          element.setAttribute("style", "visibility: visible");
        }
        this._totalStackItems++;
        this._elemList.push(element);
      });
    }
  }
  componentDidLoad() {
    core.ElementIDUtils.addIDInfo(this._hostElem);
  }
  disconectCallback() {
    this._hostElem = null;
    this._elemList = null;
  }
  render() {
    return (index.h(index.Host, null));
  }
  get _hostElem() { return index.getElement(this); }
};
EzViewStack.style = ezViewStackCss;

exports.ez_view_stack = EzViewStack;
