var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: !0 });
import { u as uuidv4, i as isKeyEvent, k as keyEvents } from "./chunk.dcf-utility.js?v=6.1.1";
class DCFButtonToggles {
  static {
    __name(this, "DCFButtonToggles");
  }
  uuid = uuidv4();
  toggleButtonElement = null;
  toggleTargetElement = null;
  toggleButtonOn = new Event(DCFButtonToggles.events("toggleButtonOn"));
  toggleButtonOff = new Event(DCFButtonToggles.events("toggleButtonOff"));
  toggleElementOn = new Event(DCFButtonToggles.events("toggleElementOn"));
  toggleElementOff = new Event(DCFButtonToggles.events("toggleElementOff"));
  toggleKeys = [];
  onKeys = [];
  offKeys = [];
  constructor(toggleButtonElement, options = {}) {
    this.toggleButtonElement = toggleButtonElement, Array.isArray(options.toggleKeys) ? this.toggleKeys = [...options.toggleKeys] : options.toggleKeys !== null && options.toggleKeys !== void 0 && options.toggleKeys !== "" && this.toggleKeys.push(options.toggleKeys), Array.isArray(options.onKeys) ? this.onKeys = [...options.onKeys] : options.onKeys !== null && options.onKeys !== void 0 && options.onKeys !== "" && this.onKeys.push(options.onKeys), Array.isArray(options.offKeys) ? this.offKeys = [...options.offKeys] : options.offKeys !== null && options.offKeys !== void 0 && options.offKeys !== "" && this.offKeys.push(options.offKeys), this.toggleKeys.includes("space") || this.toggleKeys.push("space"), this.offKeys.includes("escape") || this.offKeys.push("escape");
    const toggleElementId = this.toggleButtonElement.dataset.controls;
    if (this.toggleTargetElement = document.getElementById(toggleElementId), this.toggleTargetElement === null)
      throw new Error("Missing Toggle Element");
    let toggleButtonIdPostfix = this.toggleButtonElement.dataset.postfix;
    toggleButtonIdPostfix === void 0 && (toggleButtonIdPostfix = "");
    let toggleButtonStartExpanded = this.toggleButtonElement.dataset.startExpanded;
    if (toggleButtonStartExpanded === void 0 && (toggleButtonStartExpanded = "false"), (this.toggleButtonElement.getAttribute("id") === "" || this.toggleButtonElement.getAttribute("id") === null) && this.toggleButtonElement.setAttribute("id", this.uuid.concat("-button-", toggleButtonIdPostfix)), this.toggleButtonElement.setAttribute("aria-controls", toggleElementId), this.toggleTargetElement.getAttribute("aria-labelledby") !== null && this.toggleTargetElement.getAttribute("aria-labelledby") !== this.toggleButtonElement.getAttribute("id"))
      throw new Error("Toggle Element Already Has Toggle Button");
    this.toggleTargetElement.setAttribute("aria-labelledby", this.toggleButtonElement.getAttribute("id"));
    const expandedState = toggleButtonStartExpanded === "true" ? "open" : "close";
    this.toggleSwitched(expandedState, !0), this.#eventListeners(), this.toggleTargetElement.getAttribute("hidden") !== null && this.toggleTargetElement.removeAttribute("hidden"), this.toggleButtonElement.classList.add("dcf-btn-toggle-initialized"), this.toggleButtonElement.removeAttribute("hidden"), this.toggleButtonElement.dispatchEvent(new CustomEvent(DCFButtonToggles.events("toggleButtonReady"), {
      detail: {
        classInstance: this
      }
    })), this.toggleTargetElement.dispatchEvent(new CustomEvent(DCFButtonToggles.events("toggleButtonReady"), {
      detail: {
        classInstance: this
      }
    }));
  }
  // The names of the events to be used easily
  static events(name) {
    const events = {
      toggleButtonReady: "toggleButtonReady",
      toggleButtonOn: "toggleButtonOn",
      toggleButtonOff: "toggleButtonOff",
      toggleElementOn: "toggleElementOn",
      toggleElementOff: "toggleElementOff",
      commandClose: "commandClose",
      commandOpen: "commandOpen",
      commandToggle: "commandToggle"
    };
    return Object.freeze(events), name in events ? events[name] : void 0;
  }
  // sets up the event listeners for the button
  #eventListeners() {
    this.toggleButtonElement.addEventListener("click", (clickEvent) => {
      this.toggleSwitched() && clickEvent.preventDefault();
    }, !1), this.toggleButtonElement.addEventListener("keydown", (keydownEvent) => {
      this.toggleKeys.forEach((key) => {
        isKeyEvent(keydownEvent, keyEvents(key)) && this.toggleSwitched() && keydownEvent.preventDefault();
      }), this.onKeys.forEach((key) => {
        isKeyEvent(keydownEvent, keyEvents(key)) && this.toggleSwitched("open") && keydownEvent.preventDefault();
      }), this.offKeys.forEach((key) => {
        isKeyEvent(keydownEvent, keyEvents(key)) && this.toggleSwitched("close") && keydownEvent.preventDefault();
      });
    }, !1), this.toggleButtonElement.addEventListener(DCFButtonToggles.events("commandOpen"), () => {
      this.toggleSwitched("open");
    }), this.toggleButtonElement.addEventListener(DCFButtonToggles.events("commandClose"), () => {
      this.toggleSwitched("close");
    }), this.toggleButtonElement.addEventListener(DCFButtonToggles.events("commandToggle"), () => {
      this.toggleSwitched();
    }), this.toggleTargetElement.addEventListener(DCFButtonToggles.events("commandOpen"), () => {
      this.toggleSwitched("open");
    }), this.toggleTargetElement.addEventListener(DCFButtonToggles.events("commandClose"), () => {
      this.toggleSwitched("close");
    }), this.toggleTargetElement.addEventListener(DCFButtonToggles.events("commandToggle"), () => {
      this.toggleSwitched();
    });
  }
  // Handles the logic for the button
  // This will only call the animations
  toggleSwitched(state = "", onload = !1) {
    const toggleButtonLabelOn = this.toggleButtonElement.dataset.labelOn, toggleButtonLabelOff = this.toggleButtonElement.dataset.labelOff, timeoutTime = 10;
    return (this.toggleButtonElement.getAttribute("aria-expanded") === "false" || this.toggleButtonElement.getAttribute("aria-expanded") === null || this.toggleButtonElement.getAttribute("aria-expanded") === "") && (state === "open" || state === "") ? (this.toggleButtonElement.setAttribute("aria-expanded", "true"), "labelOff" in this.toggleButtonElement.dataset && this.toggleButtonElement.setAttribute("aria-label", toggleButtonLabelOff), this.toggleButtonElement.dispatchEvent(this.toggleButtonOn), this.toggleTargetElement.setAttribute("aria-hidden", "false"), this.toggleTargetElement.classList.remove("dcf-d-none"), setTimeout(() => {
      this.toggleTargetElement.classList.remove("dcf-opacity-0", "dcf-pointer-events-none"), this.toggleTargetElement.classList.add("dcf-opacity-100", "dcf-pointer-events-auto");
    }, timeoutTime), this.toggleTargetElement.dispatchEvent(this.toggleElementOn), this.toggleTargetElement.focus(), !0) : (this.toggleButtonElement.getAttribute("aria-expanded") === "true" || this.toggleButtonElement.getAttribute("aria-expanded") === null || this.toggleButtonElement.getAttribute("aria-expanded") === "") && (state === "close" || state === "") ? (this.toggleButtonElement.setAttribute("aria-expanded", "false"), "labelOn" in this.toggleButtonElement.dataset && this.toggleButtonElement.setAttribute("aria-label", toggleButtonLabelOn), this.toggleButtonElement.dispatchEvent(this.toggleButtonOff), this.toggleTargetElement.setAttribute("aria-hidden", "true"), this.toggleTargetElement.classList.remove("dcf-opacity-100", "dcf-pointer-events-auto"), this.toggleTargetElement.classList.add("dcf-pointer-events-none", "dcf-opacity-0"), this.#hideElement(this.toggleTargetElement, onload), this.toggleTargetElement.dispatchEvent(this.toggleElementOff), !0) : !1;
  }
  /**
   * Hides element with dcf-d-none but if it has a transition or animation it will wait for that to finish
   * @param {HTMLElement} element The element we want to hide
   * @param {Boolean} onload If we are initializing the element or not
   * @returns { Void }
   */
  #hideElement(element, onload) {
    const hasAnimation = getComputedStyle(element).animationName !== "none" || getComputedStyle(element).transitionDuration !== "0s";
    if (onload || !hasAnimation) {
      element.classList.add("dcf-d-none");
      return;
    }
    const onEnd = /* @__PURE__ */ __name(() => {
      element.classList.add("dcf-d-none");
    }, "onEnd");
    element.addEventListener("transitionend", onEnd, { once: !0 }), element.addEventListener("animationend", onEnd, { once: !0 });
  }
}
export {
  DCFButtonToggles as D
};
//# sourceMappingURL=chunk.dcf-button-toggle.js.map
