'use strict';

Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });

const lit = require('lit');
const decorators_js = require('lit/decorators.js');
const tailwindVariants = require('tailwind-variants');
const index = require('../../chunks/index-BBPgtFEP.js');
require('../lukso-icon/index.cjs');
require('../lukso-profile/index.cjs');
require('../../chunks/index-DfEZ5Urq.js');
const uniqId = require('../../chunks/uniq-id-Cszs24DQ.js');

const debounceFunction = (func, timeout = 100) => {
  let timer;
  return (...args) => {
    clearTimeout(timer);
    timer = setTimeout(() => {
      typeof func === "function" && func.apply(void 0, args);
    }, timeout);
  };
};

const style = ":host {\n    display: flex;\n    width: 100%\n}";

var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __decorateClass = (decorators, target, key, kind) => {
  var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
  for (var i = decorators.length - 1, decorator; i >= 0; i--)
    if (decorator = decorators[i])
      result = (kind ? decorator(target, key, result) : decorator(result)) || result;
  if (kind && result) __defProp(target, key, result);
  return result;
};
exports.LuksoDropdown = class LuksoDropdown extends index.TailwindStyledElement(style) {
  constructor() {
    super();
    this.id = "";
    this.triggerId = "";
    this.isOpen = false;
    this.isOpenOnOutsideClick = false;
    this.openTop = false;
    this.isRight = false;
    this.isFullWidth = false;
    this.size = "large";
    this.trigger = "click";
    this.maxHeight = void 0;
    this.styles = tailwindVariants.tv({
      slots: {
        wrapper: "absolute z-50",
        dropdown: `bg-neutral-100 border border-neutral-90 shadow-1xl
        flex flex-col gap-1 overflow-y-auto animate-fade-in animation-duration-200`
      },
      variants: {
        openTop: {
          true: {
            wrapper: "mt-0"
          }
        },
        size: {
          small: {
            dropdown: "rounded-8 p-2 mt-1 min-w-[120px] paragraph-inter-12-regular"
          },
          medium: {
            dropdown: "rounded-10 p-3 mt-2 min-w-[200px] paragraph-inter-14-regular"
          },
          large: {
            dropdown: "rounded-12 p-3 mt-2 min-w-[200px] paragraph-inter-14-regular"
          },
          "x-large": {}
        },
        isRight: {
          true: {
            wrapper: "right-0"
          }
        },
        isFullWidth: {
          true: { wrapper: "w-full", dropdown: "max-w-full w-full" },
          false: { dropdown: "w-max" }
        },
        hasMaxHeight: {
          true: {}
        }
      },
      compoundVariants: [
        {
          isFullWidth: false,
          size: "small",
          class: { dropdown: "max-w-[200px]" }
        },
        {
          isFullWidth: false,
          size: "large",
          class: { dropdown: "max-w-[300px]" }
        },
        {
          openTop: true,
          size: "small",
          class: { wrapper: "bottom-7 mb-1" }
        },
        {
          openTop: true,
          size: ["large"],
          class: { wrapper: "bottom-12 mb-2" }
        },
        {
          hasMaxHeight: false,
          size: "small",
          class: { dropdown: "max-h-52" }
        },
        {
          hasMaxHeight: false,
          size: "large",
          class: { dropdown: "max-h-64" }
        }
      ]
    });
    this.handleMouseEnter = () => {
      this.isOpen = true;
    };
    this.handleMouseLeave = (event) => {
      const relatedTarget = event.relatedTarget;
      if (relatedTarget?.id === this.id || relatedTarget?.id === this.triggerId) {
        return;
      }
      this.isOpen = false;
    };
    this.handleClick = debounceFunction((event) => {
      const element = event.target;
      const hasValidTriggerId = this.triggerId && this.triggerId.trim() !== "";
      if (hasValidTriggerId && element.id === this.triggerId || this.id === element.id) {
        this.isOpen = !this.isOpen;
        return;
      }
      if (!this.isOpenOnOutsideClick) {
        this.isOpen = false;
      }
    });
    if (!this.id) {
      this.id = uniqId.uniqId();
    }
  }
  updated(changedProperties) {
    super.updated(changedProperties);
    if (changedProperties.has("isOpen") && this.trigger === "hover") {
      const dropdownElement = this.shadowRoot?.getElementById(this.id);
      if (this.isOpen && dropdownElement) {
        dropdownElement.addEventListener("mouseenter", this.handleMouseEnter);
        dropdownElement.addEventListener("mouseleave", this.handleMouseLeave);
      } else if (dropdownElement) {
        dropdownElement.removeEventListener("mouseenter", this.handleMouseEnter);
        dropdownElement.removeEventListener("mouseleave", this.handleMouseLeave);
      }
    }
  }
  connectedCallback() {
    super.connectedCallback();
    this.boundHandleClick = this.handleClick.bind(this);
    window.addEventListener("click", this.boundHandleClick);
    if (this.trigger === "hover") {
      const triggerElement = document.getElementById(this.triggerId);
      if (triggerElement) {
        triggerElement.addEventListener("mouseenter", this.handleMouseEnter);
        triggerElement.addEventListener("mouseleave", this.handleMouseLeave);
      }
    }
  }
  disconnectedCallback() {
    super.disconnectedCallback();
    if (this.boundHandleClick) {
      window.removeEventListener("click", this.boundHandleClick);
    }
    if (this.trigger === "hover") {
      const triggerElement = document.getElementById(this.triggerId);
      if (triggerElement) {
        triggerElement.removeEventListener("mouseenter", this.handleMouseEnter);
        triggerElement.removeEventListener("mouseleave", this.handleMouseLeave);
      }
    }
  }
  async willUpdate(changedProperties) {
    if (changedProperties.has("isOpen")) {
      await this.updateComplete;
      const changeEvent = new CustomEvent(
        "on-change",
        {
          detail: {
            isOpen: this.isOpen
          },
          bubbles: false,
          composed: true
        }
      );
      this.dispatchEvent(changeEvent);
    }
  }
  render() {
    const { wrapper, dropdown } = this.styles({
      openTop: this.openTop,
      size: this.size,
      isRight: this.isRight,
      isFullWidth: this.isFullWidth,
      hasMaxHeight: this.maxHeight !== void 0
    });
    if (!this.isOpen) {
      return lit.nothing;
    }
    return lit.html`<div id=${this.id} class=${wrapper()}>
      <div
        class=${dropdown()}
        style=${this.maxHeight ? `max-height: ${this.maxHeight}px;` : lit.nothing}
      >
        <slot></slot>
      </div>
    </div>`;
  }
};
__decorateClass([
  decorators_js.property({ type: String })
], exports.LuksoDropdown.prototype, "id", 2);
__decorateClass([
  decorators_js.property({ type: String, attribute: "trigger-id" })
], exports.LuksoDropdown.prototype, "triggerId", 2);
__decorateClass([
  decorators_js.property({ type: Boolean, attribute: "is-open", reflect: true })
], exports.LuksoDropdown.prototype, "isOpen", 2);
__decorateClass([
  decorators_js.property({ type: Boolean, attribute: "is-open-on-outside-click" })
], exports.LuksoDropdown.prototype, "isOpenOnOutsideClick", 2);
__decorateClass([
  decorators_js.property({ type: Boolean, attribute: "open-top" })
], exports.LuksoDropdown.prototype, "openTop", 2);
__decorateClass([
  decorators_js.property({ type: Boolean, attribute: "is-right" })
], exports.LuksoDropdown.prototype, "isRight", 2);
__decorateClass([
  decorators_js.property({ type: Boolean, attribute: "is-full-width" })
], exports.LuksoDropdown.prototype, "isFullWidth", 2);
__decorateClass([
  decorators_js.property({ type: String })
], exports.LuksoDropdown.prototype, "size", 2);
__decorateClass([
  decorators_js.property({ type: String })
], exports.LuksoDropdown.prototype, "trigger", 2);
__decorateClass([
  decorators_js.property({ type: Number, attribute: "max-height", reflect: true })
], exports.LuksoDropdown.prototype, "maxHeight", 2);
exports.LuksoDropdown = __decorateClass([
  index.safeCustomElement("lukso-dropdown")
], exports.LuksoDropdown);
