import { prop as x, computedOf as g, Fragment as v, attr as w, WithElement as E, Value as m, OnDispose as T } from "@tempots/dom";
import { g as f, I as A } from "./timer-Rd2sKnvH.js";
function b({
  initialStatus: n = "closed",
  openedAfter: t,
  closedAfter: i
}) {
  const e = x(n), o = () => {
    e.set("start-opening");
  }, l = () => {
    e.set("start-closing");
  }, a = e.map(
    (s) => s !== "closed" && s !== "start-closing" && s !== "closing"
  ), u = (s) => {
    s ? o() : l();
  }, c = () => {
    u(!a.value);
  }, r = [], p = (s) => {
    r.push(
      e.on((P) => {
        P === "closed" && s();
      })
    );
  };
  let d = () => {
  };
  e.on((s) => {
    switch (d(), s) {
      case "start-opening":
        d = f(() => e.set("opening"));
        break;
      case "opening":
        d = t(() => e.set("opened"));
        break;
      case "start-closing":
        d = f(() => e.set("closing"));
        break;
      case "closing":
        d = i(() => e.set("closed"));
        break;
      case "closed":
        d = () => {
        };
        break;
    }
  }), e.onDispose(() => {
    r.forEach((s) => s()), d();
  });
  const k = e.map((s) => s === "closed"), O = e.map((s) => s === "start-opening"), L = e.map((s) => s === "opening"), S = e.map((s) => s === "opened"), $ = e.map((s) => s === "closing"), C = e.map((s) => s === "start-closing");
  return {
    status: e,
    open: o,
    close: l,
    toggle: c,
    setOpen: u,
    display: a,
    isClosed: k,
    isStartOpening: O,
    isOpening: L,
    isOpened: S,
    isClosing: $,
    isStartClosing: C,
    dispose: () => e.dispose(),
    listenOnClosed: p
  };
}
function K({
  initialStatus: n = "closed",
  duration: t,
  openDuration: i = t ?? 500,
  closeDuration: e = t ?? 500
} = {}) {
  return b({
    initialStatus: n,
    openedAfter: (o) => A(o, i),
    closedAfter: (o) => A(o, e)
  });
}
function y(n, t) {
  let i = !1, e = null;
  function o() {
    i || (i = !0, n.removeEventListener("transitionend", l), n.removeEventListener("animationend", a), e && (clearTimeout(e), e = null), t());
  }
  function l(r) {
    r.target === n && o();
  }
  function a(r) {
    r.target === n && o();
  }
  const c = f(() => {
    if (typeof n.getAnimations > "u") {
      e = setTimeout(o, 16);
      return;
    }
    n.addEventListener("transitionend", l), n.addEventListener("animationend", a), n.getAnimations().length === 0 && (e = setTimeout(o, 1e3));
  });
  return () => {
    i = !0, c(), e && (clearTimeout(e), e = null), n.removeEventListener("transitionend", l), n.removeEventListener("animationend", a);
  };
}
function B({
  initialStatus: n = "closed",
  element: t
} = {}) {
  let i = t;
  return {
    setElement: (e) => {
      i = e;
    },
    ...b({
      initialStatus: n,
      openedAfter: (e) => i == null ? (e(), () => {
      }) : y(i, e),
      closedAfter: (e) => i == null ? (e(), () => {
      }) : y(i, e)
    })
  };
}
function D(n) {
  if (n == null) return [];
  const t = [];
  return n.fade && t.push("fade"), n.slide && t.push(`slide-${n.slide}`), n.scale != null && t.push("scale"), t.length > 0 ? t : ["none"];
}
function N(n) {
  if (n == null) return [];
  const t = [];
  return n.scale != null && typeof n.scale == "number" && t.push(["--scale-factor", String(n.scale)]), n.transformOrigin != null && t.push(["--transform-origin", n.transformOrigin]), n.duration != null && t.push(["--animation-duration", `${n.duration}ms`]), n.easing != null && t.push(["--animation-easing", n.easing]), t;
}
function h(n, t) {
  if (n == null) return;
  const i = t === "start-opening" || t === "opening" || t === "opened";
  return "enter" in n || "exit" in n ? i ? n.enter : n.exit : n;
}
function j({
  animation: n,
  status: t
}) {
  const i = g(
    n,
    t
  )((e, o) => {
    const l = h(e, o), u = D(l).map((c) => `bc-animated-toggle--${c}`).join(" ");
    return u ? `bc-animated-toggle ${u} bc-animated-toggle--${o}` : `bc-animated-toggle bc-animated-toggle--${o}`;
  });
  return v(
    w.class(i),
    E((e) => {
      let o = [];
      function l(c) {
        for (const r of o)
          e.style.removeProperty(r);
        o = c.map(([r]) => r);
        for (const [r, p] of c)
          e.style.setProperty(r, p);
      }
      const a = g(
        n,
        t
      )((c, r) => {
        const p = h(c, r);
        return N(p);
      });
      l(m.get(a));
      const u = m.on(a, l);
      return T(() => {
        u();
        for (const c of o)
          e.style.removeProperty(c);
      });
    })
  );
}
function V({
  initialStatus: n = "closed",
  animation: t
}, i) {
  return E((e) => {
    const { setElement: o, dispose: l, ...a } = B({
      initialStatus: n,
      element: e
    });
    return o(e), v(
      j({ status: a.status, animation: t }),
      T(l),
      i(a)
    );
  });
}
export {
  j as A,
  V as a,
  b,
  K as c,
  B as u
};
