'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('../../tailwind-config.cjs');
require('../../chunks/index-oDo-wiUs.js');
const cn = require('../../chunks/cn-JMx_imgS.js');
require('viem');
require('axe-core');
require('../lukso-icon/index.cjs');
require('../lukso-form-label/index.cjs');
require('../lukso-form-description/index.cjs');
require('../lukso-form-error/index.cjs');

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.LuksoCollapse = class LuksoCollapse extends index.TailwindElement {
  constructor() {
    super(...arguments);
    this.label = "";
    this.description = "";
    this.error = "";
    this.triggerLabel = "";
    this.toggleLabel = {
      open: "",
      close: ""
    };
    this.isOpen = false;
    this.customClass = "";
    this.isDisabled = false;
    this.icon = "";
    this.size = "large";
    this.maxHeight = "0px";
    this.observedHeight = 0;
    this.onTransitionEnd = (e) => {
      if (e.propertyName !== "max-height") return;
      this.dispatchEvent(
        new CustomEvent(this.isOpen ? "after-enter" : "after-leave")
      );
    };
    this.collapseStyles = tailwindVariants.tv({
      slots: {
        base: "transition transition-all duration-150 border bg-neutral-100",
        header: "flex items-center justify-between cursor-pointer overflow-hidden",
        triggerLabel: "",
        toggleLabel: "",
        icon: "transition cursor-pointer",
        content: "transition-all duration-250 ease-in-out"
      },
      variants: {
        isOpen: {
          true: {
            icon: "rotate-180",
            content: "opacity-100"
          },
          false: {
            content: "opacity-0"
          }
        },
        isDisabled: {
          true: {
            base: "opacity-60",
            header: "cursor-not-allowed",
            icon: "opacity-60 cursor-not-allowed"
          }
        },
        size: {
          small: {
            base: "rounded-8",
            header: "h-7 pl-2",
            triggerLabel: "paragraph-inter-12-semi-bold",
            toggleLabel: "paragraph-inter-12-semi-bold",
            icon: "mr-1"
          },
          medium: {
            base: "rounded-10",
            header: "h-10 pl-3",
            triggerLabel: "paragraph-inter-14-semi-bold",
            toggleLabel: "paragraph-inter-14-semi-bold",
            icon: "mr-2"
          },
          large: {
            base: "rounded-12",
            header: "h-12 pl-4",
            triggerLabel: "paragraph-inter-14-semi-bold",
            toggleLabel: "paragraph-inter-14-semi-bold",
            icon: "mr-3"
          },
          "x-large": {
            base: "rounded-14",
            header: "h-17 pl-5",
            triggerLabel: "paragraph-inter-16-semi-bold",
            toggleLabel: "paragraph-inter-16-semi-bold",
            icon: "mr-4"
          }
        },
        hasError: {
          true: {
            base: "border-red-85 hover:border-red-65",
            triggerLabel: "text-red-65",
            toggleLabel: "text-red-65"
          },
          false: {
            base: "border-neutral-90",
            triggerLabel: "text-neutral-45",
            toggleLabel: "text-neutral-45"
          }
        }
      },
      compoundVariants: [
        {
          isDisabled: false,
          hasError: false,
          class: {
            base: "hover:border-neutral-35"
          }
        }
      ]
    });
  }
  firstUpdated() {
    if ("ResizeObserver" in window) {
      this.resizeObserver = new ResizeObserver(() => this.syncHeight());
      this.resizeObserver.observe(this.contentElement);
    }
    this.syncHeight(true);
  }
  disconnectedCallback() {
    this.resizeObserver?.disconnect();
    super.disconnectedCallback();
  }
  updated(changed) {
    if (changed.has("isOpen")) {
      this.syncHeight();
    }
  }
  syncHeight(immediate = false) {
    const content = this.contentElement;
    if (!content) return;
    const height = content.scrollHeight;
    this.observedHeight = height;
    if (this.isOpen) {
      if (immediate) {
        this.maxHeight = `${height}px`;
      } else {
        if (this.maxHeight !== `${height}px`) this.maxHeight = `${height}px`;
      }
    } else {
      this.maxHeight = `${height}px`;
      requestAnimationFrame(() => {
        this.maxHeight = "0px";
      });
    }
  }
  toggle() {
    this.isOpen = !this.isOpen;
    this.dispatchEvent(
      new CustomEvent("toggle", { detail: { open: this.isOpen } })
    );
  }
  render() {
    const { base, header, triggerLabel, toggleLabel, icon, content } = this.collapseStyles({
      isOpen: this.isOpen,
      isDisabled: this.isDisabled,
      size: this.size,
      hasError: !!this.error
    });
    return lit.html`
      <div class="w-[inherit]">
        <lukso-form-label label=${this.label}></lukso-form-label>
        <lukso-form-description
          description=${this.description}
        ></lukso-form-description>
        <div class=${cn.cn(base(), this.customClass)}>
          <!-- Header -->
          <div
            class=${header()}
            @click=${() => !this.isDisabled && this.toggle()}
          >
            <span class=${triggerLabel()}>${this.triggerLabel}</span>
            <div class="flex items-center">
              ${this.toggleLabel ? lit.html`<span class=${toggleLabel()}>
                    ${this.isOpen ? this.toggleLabel?.close : this.toggleLabel?.open}
                  </span>` : lit.nothing}
              ${this.icon ? lit.html`<lukso-icon
                    name=${this.icon}
                    class=${icon()}
                  ></lukso-icon>` : lit.nothing}
            </div>
          </div>

          <!-- Content -->
          <div
            class=${content()}
            style="max-height:${this.maxHeight};"
            @transitionend=${this.onTransitionEnd}
          >
            <div class="collapse-content">
              <slot @slotchange=${() => this.syncHeight()}></slot>
            </div>
          </div>
        </div>
        <lukso-form-error error=${this.error}></lukso-form-error>
      </div>
    `;
  }
};
__decorateClass([
  decorators_js.property({ type: String })
], exports.LuksoCollapse.prototype, "label", 2);
__decorateClass([
  decorators_js.property({ type: String })
], exports.LuksoCollapse.prototype, "description", 2);
__decorateClass([
  decorators_js.property({ type: String })
], exports.LuksoCollapse.prototype, "error", 2);
__decorateClass([
  decorators_js.property({ type: String, attribute: "trigger-label" })
], exports.LuksoCollapse.prototype, "triggerLabel", 2);
__decorateClass([
  decorators_js.property({ type: Object, attribute: "toggle-label" })
], exports.LuksoCollapse.prototype, "toggleLabel", 2);
__decorateClass([
  decorators_js.property({ type: Boolean, attribute: "is-open" })
], exports.LuksoCollapse.prototype, "isOpen", 2);
__decorateClass([
  decorators_js.property({ type: String, attribute: "custom-class" })
], exports.LuksoCollapse.prototype, "customClass", 2);
__decorateClass([
  decorators_js.property({ type: Boolean, attribute: "is-disabled" })
], exports.LuksoCollapse.prototype, "isDisabled", 2);
__decorateClass([
  decorators_js.property({ type: String, attribute: "icon" })
], exports.LuksoCollapse.prototype, "icon", 2);
__decorateClass([
  decorators_js.property({ type: String })
], exports.LuksoCollapse.prototype, "size", 2);
__decorateClass([
  decorators_js.state()
], exports.LuksoCollapse.prototype, "maxHeight", 2);
__decorateClass([
  decorators_js.state()
], exports.LuksoCollapse.prototype, "observedHeight", 2);
__decorateClass([
  decorators_js.query(".collapse-content")
], exports.LuksoCollapse.prototype, "contentElement", 2);
exports.LuksoCollapse = __decorateClass([
  index.safeCustomElement("lukso-collapse")
], exports.LuksoCollapse);
