'use strict';

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

const index = require('./index-a7b0c73d.js');
const core = require('@sankhyalabs/core');
require('./ApplicationUtils-5cc05979.js');
const CSSVarsUtils = require('./CSSVarsUtils-f20973d1.js');
require('./DialogType-2114c337.js');
require('./CheckMode-ecb90b87.js');
require('./index-1be40f19.js');
require('./FormLayout-18853e70.js');

const ezActionsButtonCss = ":host{--ez-actions-button__actions-list--border-radius:var(--border--radius-medium, 12px);--ez-actions-button__actions-list--box-shadow:var(--shadow, 0px 0px 16px 0px #000);--ez-actions-button__actions-list--background-color:var(--background--xlight, #fff);--ez-actions-button__actions-list--padding:var(--space--small, 6px);--ez-actions-button__actions-list--top-margin:var(--space-small, 6px);--ez-actions-button__actions-list--z-index:var(--ez-elevation--8, 8);--ez-actions-button__actions-max-height:415px;--ez-actions-button__btn-action--min-width:'auto';--ez-actions-button__btn-action--background-color:var(--background--xlight, #fff);display:flex;flex-direction:column;height:fit-content;user-select:none}.ez-actions-button__actions-list{display:flex;flex-direction:column;position:fixed;width:fit-content;height:fit-content;overflow-y:auto;scrollbar-width:thin;z-index:var(--ez-actions-button__actions-list--z-index);padding:var(--ez-actions-button__actions-list--padding);margin-top:var(--ez-actions-button__actions-list--top-margin);background-color:var(--ez-actions-button__actions-list--background-color);border-radius:var(--ez-actions-button__actions-list--border-radius);box-shadow:var(--ez-actions-button__actions-list--box-shadow)}.ez-actions-button__actions-list--max-height{max-height:var(--ez-actions-button__actions-max-height)}.ez-actions-button__actions-list--lowered{margin-top:calc(var(--ez-actions-button__actions-list--top-margin) + 6px)}.ez-actions-button__btn-action{--ez-button--justify-content:flex-start;--ez-button--width:100%;--ez-button--min-width:var(--ez-actions-button__btn-action--min-width);--ez-button--background-color:var(--ez-actions-button__btn-action--background-color);--ez-button--font-weight:var(--text-weight--medium, 400);--ez-button--padding-left:var(--space--medium, 12px);--ez-button--padding-right:var(--space--medium, 12px);--ez-button--border:none}.ez-actions-button__btn-action--spaced{--ez-button--padding-left:calc(var(--space--medium, 12px) + 24px)}.ez-actions-button__icon-right{margin-left:var(--space--small, 6px)}.ez-actions-button__icon-check,.ez-actions-button__icon-item{position:absolute;left:var(--space--medium, 12px)}.ez-actions-button__icon-check{color:var(--ez-button--hover-color)}.ez-actions-button__arrow{position:absolute;border-left:10px solid transparent;border-right:10px solid transparent;width:0;height:0;z-index:calc(var(--ez-actions-button__actions-list--z-index) + 1);border-bottom:15px solid var(--ez-actions-button__btn-action--background-color)}.ez-actions-button__arrow--upped{margin-top:calc((var(--ez-actions-button__actions-list--top-margin) + 2px) * -1)}.ez-actions-button__arrow--small{margin-left:6px}.ez-actions-button__arrow--medium{margin-left:11px}.ez-actions-button__arrow--large{margin-left:13px}.ez-actions-button__arrow:only-child{display:none}.ez-actions-button__btn-transparent{--ez-button--background-color:transparent;--ez-button--hover--background-color:transparent;--ez-button--active--background-color:transparent;--ez-button--focus--border:none;--ez-button--border:none}.ez-actions-button__btn-label{--ez-button--padding-left:var(--space--medium, 12px);--ez-button--padding-right:var(--space--medium, 12px)}.ez-actions-button__list-container{position:relative}.ez-actions-button--bottom-padding{padding-bottom:var(--space--small, 6px)}";

const EzActionsButton = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.ezAction = index.createEvent(this, "ezAction", 7);
    this.ezPopoverOpen = index.createEvent(this, "ezPopoverOpen", 7);
    this.ezDisconnectedActionButtons = index.createEvent(this, "ezDisconnectedActionButtons", 7);
    this._arrowOffset = 5;
    this.innerClickCheck = (floatingContainer, node) => {
      var _a;
      if (node && floatingContainer) {
        if (node === floatingContainer) {
          return true;
        }
        const children = floatingContainer.children;
        for (let i = 0; i < children.length; i++) {
          if ((_a = children[i].shadowRoot) === null || _a === void 0 ? void 0 : _a.contains(node)) {
            return true;
          }
        }
        do {
          if (floatingContainer.contains(node)) {
            return true;
          }
          node = node.offsetParent;
        } while (node != undefined && node != document.getRootNode());
      }
      return false;
    };
    this._selectedAction = undefined;
    this.enabled = true;
    this.actions = undefined;
    this.size = "medium";
    this.showLabel = false;
    this.displayIcon = undefined;
    this.checkOption = false;
    this.value = undefined;
    this.isTransparent = false;
    this.arrowActive = false;
  }
  /**
   * Oculta a lista de ações.
   */
  async hideActions() {
    if (this._floatingID != undefined) {
      core.FloatingManager.close(this._floatingID);
    }
    this._floatingID = undefined;
  }
  /**
   * Apresenta a lista de ações.
   */
  async showActions() {
    if (!this.enabled) {
      return;
    }
    const options = this.getFloatOptions();
    this._floatingID = core.FloatingManager.float(this._actionsList, this._listContainer, options);
    const sideLimit = this.getSideLimit();
    if (sideLimit != undefined) {
      options.left = sideLimit;
      core.FloatingManager.updateFloatPosition(this._actionsList, this._listContainer, options);
    }
    window.requestAnimationFrame(() => {
      this._actionsList.scrollIntoView({ behavior: "smooth", block: "nearest", inline: "nearest" });
    });
    this.ezPopoverOpen.emit(this._actionsList);
  }
  /**
   * Verifica se a lista de ações está aberta.
   */
  async isOpened() {
    return this._floatingID != undefined;
  }
  getFloatOptions() {
    return {
      autoClose: true,
      innerClickTest: this.innerClickCheck,
      isFixed: true,
      top: this.getPositionTop(),
      left: this.getPositionLeft(),
      backClickListener: () => this._floatingID = undefined
    };
  }
  getSideLimit() {
    var _a;
    const docWidth = document.body.clientWidth;
    const boundingList = (_a = this._actionsList) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect();
    const rightOffset = docWidth - this._button.getBoundingClientRect().right;
    if ((boundingList === null || boundingList === void 0 ? void 0 : boundingList.right) >= docWidth || (boundingList === null || boundingList === void 0 ? void 0 : boundingList.right) === (docWidth - rightOffset)) {
      return (docWidth - boundingList.width - rightOffset) + "px";
    }
    return;
  }
  updatePosition() {
    if (!this.enabled || this._floatingID == undefined) {
      return;
    }
    const options = this.getFloatOptions();
    const sideLimit = this.getSideLimit();
    if (sideLimit != undefined) {
      options.left = sideLimit;
    }
    core.FloatingManager.updateFloatPosition(this._actionsList, this._listContainer, options);
  }
  getPositionTop() {
    var _a;
    const boundingHost = (_a = this._element) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect();
    if (boundingHost == undefined) {
      return null;
    }
    return (boundingHost.y + boundingHost.height) + "px";
  }
  getBoundingLeft() {
    var _a;
    const boundingHost = (_a = this._element) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect();
    if (boundingHost == undefined) {
      return null;
    }
    const arrowOffset = this.arrowActive ? this._arrowOffset : 0;
    return (boundingHost.x - arrowOffset) + "px";
  }
  getPositionLeft() {
    const boundingLeft = this.getBoundingLeft();
    if (boundingLeft) {
      return boundingLeft;
    }
    else if (this.arrowActive) {
      return `-${this._arrowOffset}px`;
    }
    return null;
  }
  hasLabelOrCheckOption() {
    var _a;
    return (this.showLabel || this.checkOption) && ((_a = this.actions) === null || _a === void 0 ? void 0 : _a.length) > 0;
  }
  hasIconName() {
    var _a;
    return (_a = this.actions) === null || _a === void 0 ? void 0 : _a.some(action => action.iconName != undefined);
  }
  controlScrollPage() {
    window.removeEventListener("scroll", this.updatePosition.bind(this));
    window.addEventListener("scroll", this.updatePosition.bind(this));
  }
  //---------------------------------------------
  // Event handlers
  //---------------------------------------------
  handlerButtonClick() {
    this.showActions();
  }
  actionClick(action) {
    this._selectedAction = action;
    this.hideActions();
    this.ezAction.emit(action);
  }
  //---------------------------------------------
  // Lifecycle web component
  //---------------------------------------------
  componentWillLoad() {
    if (this.actions == undefined) {
      this.actions = [];
      const optionsTags = this._element.querySelectorAll("action");
      if (optionsTags) {
        optionsTags.forEach((e) => {
          let label = e.innerText;
          let value = e.getAttribute("value");
          let enabled = !(e.getAttribute("enabled") === "false");
          if (!value) {
            value = label;
          }
          this.actions.push({ label, value, enabled });
          e.hidden = true;
        });
      }
    }
  }
  disconnectedCallback() {
    this.ezDisconnectedActionButtons.emit();
  }
  componentDidLoad() {
    CSSVarsUtils.CSSVarsUtils.applyVarsButton(this._element, this._button);
    core.ElementIDUtils.addIDInfo(this._element);
    this.controlScrollPage();
  }
  componentDidRender() {
    var _a;
    if (this._floatingID == undefined) {
      (_a = this._actionsList) === null || _a === void 0 ? void 0 : _a.remove();
    }
    if (this.hasLabelOrCheckOption()) {
      if (!!this.value) {
        this._selectedAction = this.actions.find((action) => action.value === this.value);
      }
      else if (!!!this._selectedAction) {
        this._selectedAction = this.actions[0];
      }
    }
  }
  buildActionElement(action) {
    var _a;
    if (!action) {
      return;
    }
    if (action.itemBuilder != undefined) {
      const customContent = action.itemBuilder(this._element, action);
      if (typeof customContent === "string") {
        return index.h("div", { class: "ez-actions-button__btn-action", innerHTML: customContent });
      }
      return customContent;
    }
    return index.h("ez-button", { size: "small", label: action.label, onClick: () => this.actionClick(action), enabled: action.enabled, class: "ez-actions-button__btn-action" +
        (this.checkOption || this.hasIconName() ? " ez-actions-button__btn-action--spaced" : "") }, this.checkOption && ((_a = this._selectedAction) === null || _a === void 0 ? void 0 : _a.value) === action.value &&
      index.h("ez-icon", { class: "ez-actions-button__icon-check", slot: "leftIcon", size: "small", iconName: "check" }), !this.checkOption && action.iconName &&
      index.h("ez-icon", { class: "ez-actions-button__icon-item", slot: "leftIcon", size: "small", iconName: action.iconName }));
  }
  render() {
    var _a;
    return (index.h(index.Host, null, index.h("ez-button", { ref: elem => this._button = elem, class: (this.isTransparent ? "ez-actions-button__btn-transparent" : "") +
        (this.showLabel ? " ez-actions-button__btn-label" : ""), label: this.showLabel && ((_a = this._selectedAction) === null || _a === void 0 ? void 0 : _a.label), enabled: this.enabled, mode: this.showLabel ? undefined : "icon", iconName: this.showLabel ? "" : this.displayIcon || "dots-vertical", size: this.size, onClick: () => this.handlerButtonClick() }, this.showLabel &&
      index.h("ez-icon", { class: "ez-actions-button__icon-right", slot: "rightIcon", iconName: this.displayIcon || "dots-vertical" })), index.h("section", { class: "ez-actions-button__list-container", ref: elem => this._listContainer = elem }, this.arrowActive &&
      index.h("div", { class: "ez-actions-button__arrow ez-actions-button__arrow--" + (this.size || "small") +
          (this.isTransparent ? " ez-actions-button__arrow--upped" : "") }), index.h("div", { ref: elem => this._actionsList = elem, class: "ez-actions-button__actions-list ez-actions-button__actions-list--max-height" +
        (this.arrowActive && !this.isTransparent ? " ez-actions-button__actions-list--lowered" : "") }, this.actions.map(action => this.buildActionElement(action))))));
  }
  get _element() { return index.getElement(this); }
};
EzActionsButton.style = ezActionsButtonCss;

exports.ez_actions_button = EzActionsButton;
