"use strict";
var A = Object.defineProperty;
var le = Object.getOwnPropertyDescriptor;
var ce = Object.getOwnPropertyNames;
var de = Object.prototype.hasOwnProperty;
var d = (n, e) => A(n, "name", { value: e, configurable: !0 });
var ae = (n, e) => {
  for (var l in e)
    A(n, l, { get: e[l], enumerable: !0 });
}, pe = (n, e, l, c) => {
  if (e && typeof e == "object" || typeof e == "function")
    for (let r of ce(e))
      !de.call(n, r) && r !== l && A(n, r, { get: () => e[r], enumerable: !(c = le(e, r)) || c.enumerable });
  return n;
};
var ue = (n) => pe(A({}, "__esModule", { value: !0 }), n);

// src/highlight/preview.ts
var xe = {};
ae(xe, {
  default: () => ye
});
module.exports = ue(xe);
var B = require("storybook/preview-api");

// src/highlight/useHighlights.ts
var oe = require("storybook/internal/core-events");

// src/highlight/constants.ts
var R = "storybook/highlight", z = `${R}/add`, j = `${R}/remove`, Z = `${R}/reset`, W = `${R}/scroll-into-view`, _ = 2147483647;

// src/highlight/utils.ts
var me = "svg,path,rect,circle,line,polyline,polygon,ellipse,text".split(","), x = /* @__PURE__ */ d((n, e, l) => {
  let c = me.includes(n) ? document.createElementNS("http://www.w3.org/2000/svg", n) : document.createElement(n);
  return Object.entries(e).forEach(([r, p]) => {
    /[A-Z]/.test(r) ? (r === "onClick" && (c.addEventListener("click", p), c.addEventListener("keydown", (u) => {
      (u.key === "Enter" || u.key === " ") && (u.preventDefault(), p());
    })), r === "onMouseEnter" && c.addEventListener("mouseenter", p), r === "onMouseLeave" && c.addEventListener("mouseleave", p)) : c.setAttribute(
    r, p);
  }), l?.forEach((r) => {
    if (!(r == null || r === !1))
      try {
        c.appendChild(r);
      } catch {
        c.appendChild(document.createTextNode(String(r)));
      }
  }), c;
}, "createElement"), K = /* @__PURE__ */ d((n) => {
  if ("elements" in n) {
    let { elements: e, color: l, style: c } = n;
    return {
      selectors: e,
      styles: {
        outline: `2px ${c} ${l}`,
        outlineOffset: "2px",
        boxShadow: "0 0 0 6px rgba(255,255,255,0.6)"
      }
    };
  }
  return {
    styles: {
      outline: "2px dashed #029cfd"
    },
    ...n
  };
}, "convertLegacy"), ge = /* @__PURE__ */ d((n) => n instanceof Function, "isFunction"), S = /* @__PURE__ */ new Map(), I = /* @__PURE__ */ new Map(),
D = /* @__PURE__ */ new Map(), T = /* @__PURE__ */ d((n) => {
  let e = Symbol();
  return I.set(e, []), S.set(e, n), { get: /* @__PURE__ */ d(() => S.get(e), "get"), set: /* @__PURE__ */ d((u) => {
    let m = S.get(e), g = ge(u) ? u(m) : u;
    g !== m && (S.set(e, g), I.get(e)?.forEach((w) => {
      D.get(w)?.(), D.set(w, w(g));
    }));
  }, "set"), subscribe: /* @__PURE__ */ d((u) => (I.get(e)?.push(u), () => {
    let m = I.get(e);
    m && I.set(
      e,
      m.filter((g) => g !== u)
    );
  }), "subscribe"), teardown: /* @__PURE__ */ d(() => {
    I.get(e)?.forEach((u) => {
      D.get(u)?.(), D.delete(u);
    }), I.delete(e), S.delete(e);
  }, "teardown") };
}, "useStore"), N = /* @__PURE__ */ d((n) => {
  let e = document.getElementById("storybook-root"), l = /* @__PURE__ */ new Map();
  for (let c of n) {
    let { priority: r = 0 } = c;
    for (let p of c.selectors) {
      let u = [
        ...document.querySelectorAll(
          // Elements matching the selector, excluding storybook elements and their descendants.
          // Necessary to find portaled elements (e.g. children of `body`).
          `:is(${p}):not([id^="storybook-"], [id^="storybook-"] *, [class^="sb-"], [class^="sb-"] *)`
        ),
        // Elements matching the selector inside the storybook root, as these were excluded above.
        ...e?.querySelectorAll(p) || []
      ];
      for (let m of u) {
        let g = l.get(m);
        (!g || g.priority <= r) && l.set(m, {
          ...c,
          priority: r,
          selectors: Array.from(new Set((g?.selectors || []).concat(p)))
        });
      }
    }
  }
  return l;
}, "mapElements"), U = /* @__PURE__ */ d((n) => Array.from(n.entries()).map(([e, { selectors: l, styles: c, hoverStyles: r, focusStyles: p, menu: u }]) => {
  let { top: m, left: g, width: w, height: E } = e.getBoundingClientRect(), { position: v } = getComputedStyle(e);
  return {
    element: e,
    selectors: l,
    styles: c,
    hoverStyles: r,
    focusStyles: p,
    menu: u,
    top: v === "fixed" ? m : m + window.scrollY,
    left: v === "fixed" ? g : g + window.scrollX,
    width: w,
    height: E
  };
}).sort((e, l) => l.width * l.height - e.width * e.height), "mapBoxes"), G = /* @__PURE__ */ d((n, e) => {
  let l = n.getBoundingClientRect(), { x: c, y: r } = e;
  return l?.top && l?.left && c >= l.left && c <= l.left + l.width && r >= l.top && r <= l.top + l.height;
}, "isOverMenu"), P = /* @__PURE__ */ d((n, e, l) => {
  if (!e || !l)
    return !1;
  let { left: c, top: r, width: p, height: u } = n;
  (u === 0 || p === 0) && (c -= 10, r -= 10, p += 20, u += 20), e.style.position === "fixed" && (c += window.scrollX, r += window.scrollY);
  let { x: m, y: g } = l;
  return m >= c && m <= c + p && g >= r && g <= r + u;
}, "isTargeted"), J = /* @__PURE__ */ d((n, e, l = {}) => {
  let { x: c, y: r } = e, { margin: p = 5, topOffset: u = 0, centered: m = !1 } = l, { scrollX: g, scrollY: w, innerHeight: E, innerWidth: v } = window,
  M = Math.min(
    n.style.position === "fixed" ? r - w : r,
    E - n.clientHeight - p - u + w
  ), H = m ? n.clientWidth / 2 : 0, C = n.style.position === "fixed" ? Math.max(Math.min(c - g, v - H - p), H + p) : Math.max(
    Math.min(c, v - H - p + g),
    H + p + g
  );
  Object.assign(n.style, {
    ...C !== c && { left: `${C}px` },
    ...M !== r && { top: `${M}px` }
  });
}, "keepInViewport"), X = /* @__PURE__ */ d((n) => {
  window.HTMLElement.prototype.hasOwnProperty("showPopover") && n.showPopover();
}, "showPopover"), Q = /* @__PURE__ */ d((n) => {
  window.HTMLElement.prototype.hasOwnProperty("showPopover") && n.hidePopover();
}, "hidePopover"), ee = /* @__PURE__ */ d((n) => ({
  top: n.top,
  left: n.left,
  width: n.width,
  height: n.height,
  selectors: n.selectors,
  element: {
    attributes: Object.fromEntries(
      Array.from(n.element.attributes).map((e) => [e.name, e.value])
    ),
    localName: n.element.localName,
    tagName: n.element.tagName,
    outerHTML: n.element.outerHTML
  }
}), "getEventDetails");

// src/highlight/useHighlights.ts
var f = "storybook-highlights-menu", te = "storybook-highlights-root", he = "storybook-root", fe = /* @__PURE__ */ d(() => x(
  "svg",
  { width: "14", height: "14", viewBox: "0 0 14 14", xmlns: "http://www.w3.org/2000/svg" },
  [
    x("path", {
      fillRule: "evenodd",
      clipRule: "evenodd",
      d: "M9.10355 10.1464C9.29882 10.3417 9.29882 10.6583 9.10355 10.8536C8.90829 11.0488 8.59171 11.0488 8.39645 10.8536L4.89645 7.35355C4\
.70118 7.15829 4.70118 6.84171 4.89645 6.64645L8.39645 3.14645C8.59171 2.95118 8.90829 2.95118 9.10355 3.14645C9.29882 3.34171 9.29882 3.658\
29 9.10355 3.85355L5.95711 7L9.10355 10.1464Z",
      fill: "currentColor"
    })
  ]
), "chevronLeft"), be = /* @__PURE__ */ d(() => x(
  "svg",
  { width: "14", height: "14", viewBox: "0 0 14 14", xmlns: "http://www.w3.org/2000/svg" },
  [
    x("path", {
      fillRule: "evenodd",
      clipRule: "evenodd",
      d: "M4.89645 10.1464C4.70118 10.3417 4.70118 10.6583 4.89645 10.8536C5.09171 11.0488 5.40829 11.0488 5.60355 10.8536L9.10355 7.35355C9\
.29882 7.15829 9.29882 6.84171 9.10355 6.64645L5.60355 3.14645C5.40829 2.95118 5.09171 2.95118 4.89645 3.14645C4.70118 3.34171 4.70118 3.658\
29 4.89645 3.85355L8.04289 7L4.89645 10.1464Z",
      fill: "currentColor"
    })
  ]
), "chevronRight"), ne = /* @__PURE__ */ d((n) => {
  if (globalThis.__STORYBOOK_HIGHLIGHT_INITIALIZED)
    return;
  globalThis.__STORYBOOK_HIGHLIGHT_INITIALIZED = !0;
  let { document: e } = globalThis, l = T([]), c = T(/* @__PURE__ */ new Map()), r = T([]), p = T(), u = T(), m = T([]), g = T([]), w = T(),
  E = T(), v = e.getElementById(te);
  l.subscribe(() => {
    v || (v = x("div", { id: te }), e.body.appendChild(v));
  }), l.subscribe((s) => {
    let t = e.getElementById(he);
    if (!t)
      return;
    c.set(N(s));
    let o = new MutationObserver(() => c.set(N(s)));
    return o.observe(t, { subtree: !0, childList: !0 }), () => {
      o.disconnect();
    };
  }), c.subscribe((s) => {
    let t = /* @__PURE__ */ d(() => requestAnimationFrame(() => r.set(U(s))), "updateBoxes"), o = new ResizeObserver(t);
    o.observe(e.body), Array.from(s.keys()).forEach((i) => o.observe(i));
    let a = Array.from(e.body.querySelectorAll("*")).filter((i) => {
      let { overflow: h, overflowX: b, overflowY: y } = window.getComputedStyle(i);
      return ["auto", "scroll"].some((L) => [h, b, y].includes(L));
    });
    return a.forEach((i) => i.addEventListener("scroll", t)), () => {
      o.disconnect(), a.forEach((i) => i.removeEventListener("scroll", t));
    };
  }), c.subscribe((s) => {
    let t = Array.from(s.keys()).filter(({ style: a }) => a.position === "sticky"), o = /* @__PURE__ */ d(() => requestAnimationFrame(() => {
      r.set(
        (a) => a.map((i) => {
          if (t.includes(i.element)) {
            let { top: h, left: b } = i.element.getBoundingClientRect();
            return { ...i, top: h + window.scrollY, left: b + window.scrollX };
          }
          return i;
        })
      );
    }), "updateBoxes");
    return e.addEventListener("scroll", o), () => e.removeEventListener("scroll", o);
  }), c.subscribe((s) => {
    m.set((t) => t.filter(({ element: o }) => s.has(o)));
  }), m.subscribe((s) => {
    s.length ? (E.set((t) => s.some((o) => o.element === t?.element) ? t : void 0), w.set((t) => s.some((o) => o.element === t?.element) ? t :
    void 0)) : (E.set(void 0), w.set(void 0), p.set(void 0));
  });
  let M = new Map(/* @__PURE__ */ new Map());
  l.subscribe((s) => {
    s.forEach(({ keyframes: t }) => {
      if (t) {
        let o = M.get(t);
        o || (o = e.createElement("style"), o.setAttribute("data-highlight", "keyframes"), M.set(t, o), e.head.appendChild(o)), o.innerHTML =
        t;
      }
    }), M.forEach((t, o) => {
      s.some((a) => a.keyframes === o) || (t.remove(), M.delete(o));
    });
  });
  let H = new Map(/* @__PURE__ */ new Map());
  r.subscribe((s) => {
    s.forEach((t) => {
      let o = H.get(t.element);
      if (v && !o) {
        let a = {
          popover: "manual",
          "data-highlight-dimensions": `w${t.width.toFixed(0)}h${t.height.toFixed(0)}`,
          "data-highlight-coordinates": `x${t.left.toFixed(0)}y${t.top.toFixed(0)}`
        };
        o = v.appendChild(x("div", a)), H.set(t.element, o);
      }
    }), H.forEach((t, o) => {
      s.some(({ element: a }) => a === o) || (t.remove(), H.delete(o));
    });
  }), r.subscribe((s) => {
    let t = s.filter((a) => a.menu);
    if (!t.length)
      return;
    let o = /* @__PURE__ */ d((a) => {
      requestAnimationFrame(() => {
        let i = e.getElementById(f), h = { x: a.pageX, y: a.pageY };
        if (i && !G(i, h)) {
          let b = t.filter((y) => {
            let L = H.get(y.element);
            return P(y, L, h);
          });
          p.set(b.length ? h : void 0), m.set(b);
        }
      });
    }, "onClick");
    return e.addEventListener("click", o), () => e.removeEventListener("click", o);
  });
  let C = /* @__PURE__ */ d(() => {
    let s = e.getElementById(f), t = u.get();
    !t || s && G(s, t) || g.set((o) => {
      let a = r.get().filter((y) => {
        let L = H.get(y.element);
        return P(y, L, t);
      }), i = o.filter((y) => a.includes(y)), h = a.filter((y) => !o.includes(y)), b = o.length - i.length;
      return h.length || b ? [...i, ...h] : o;
    });
  }, "updateHovered");
  u.subscribe(C), r.subscribe(C);
  let O = /* @__PURE__ */ d(() => {
    let s = E.get(), t = s ? [s] : m.get(), o = t.length === 1 ? t[0] : w.get(), a = p.get() !== void 0;
    r.get().forEach((i) => {
      let h = H.get(i.element);
      if (h) {
        let b = o === i, y = a ? o ? b : t.includes(i) : g.get()?.includes(i);
        Object.assign(h.style, {
          animation: "none",
          background: "transparent",
          border: "none",
          boxSizing: "border-box",
          outline: "none",
          outlineOffset: "0px",
          ...i.styles,
          ...y ? i.hoverStyles : {},
          ...b ? i.focusStyles : {},
          position: getComputedStyle(i.element).position === "fixed" ? "fixed" : "absolute",
          zIndex: _ - 10,
          top: `${i.top}px`,
          left: `${i.left}px`,
          width: `${i.width}px`,
          height: `${i.height}px`,
          margin: 0,
          padding: 0,
          cursor: i.menu ? "pointer" : "default",
          pointerEvents: i.menu ? "auto" : "none"
        }), X(h);
      }
    });
  }, "updateBoxStyles");
  r.subscribe(O), m.subscribe(O), g.subscribe(O), w.subscribe(O), E.subscribe(O);
  let Y = /* @__PURE__ */ d(() => {
    if (!v)
      return;
    let s = e.getElementById(f);
    if (s)
      s.innerHTML = "";
    else {
      let i = { id: f, popover: "manual" };
      s = v.appendChild(x("div", i)), v.appendChild(
        x("style", {}, [
          `
            #${f} {
              position: absolute;
              z-index: ${_};
              width: 300px;
              padding: 0px;
              margin: 15px 0 0 0;
              transform: translateX(-50%);
              font-family: "Nunito Sans", -apple-system, ".SFNSText-Regular", "San Francisco", BlinkMacSystemFont, "Segoe UI", "Helvetica Ne\
ue", Helvetica, Arial, sans-serif;
              font-size: 12px;
              background: white;
              border: none;
              border-radius: 6px;
              box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.05), 0 5px 15px 0 rgba(0, 0, 0, 0.1);
              color: #2E3438;
            }
            #${f} ul {
              list-style: none;
              padding: 4px 0;
              margin: 0;
              max-height: 300px;
              overflow-y: auto;
            }
            #${f} li {
              padding: 0 4px;
              margin: 0;
            }
            #${f} li > * {
              display: flex;
              padding: 8px;
              margin: 0;
              align-items: center;
              gap: 8px;
              border-radius: 4px;
            }
            #${f} button {
              width: 100%;
              border: 0;
              background: transparent;
              color: inherit;
              text-align: left;
              font-family: inherit;
              font-size: inherit;
            }
            #${f} button:focus {
              outline-color: #029CFD;
            }
            #${f} button:hover {
              background: rgba(2, 156, 253, 0.07);
              color: #029CFD;
              cursor: pointer;
            }
            #${f} li code {
              white-space: nowrap;
              overflow: hidden;
              text-overflow: ellipsis;
              line-height: 16px;
              font-size: 11px;
            }
            #${f} li svg {
              display: none;
              flex-shrink: 0;
              margin: 1px;
              color: #73828C;
            }
            #${f} li > button:hover svg, #${f} li > button:focus svg {
              color: #029CFD;
            }
            #${f} li.selectable svg, #${f} li.selected svg {
              display: block;
            }
            #${f} .menu-list {
              border-top: 1px solid rgba(38, 85, 115, 0.15);
            }
            #${f} .menu-list li:not(:last-child) {
              padding-bottom: 4px;
              margin-bottom: 4px;
              border-bottom: 1px solid rgba(38, 85, 115, 0.15);
            }
            #${f} .menu-list li div {
              display: flex;
              flex-direction: column;
              align-items: flex-start;
              gap: 0;
            }
            #${f} .menu-list li small {
              color: #5C6870;
              font-size: 11px;
            }
          `
        ])
      );
    }
    let t = E.get(), o = t ? [t] : m.get();
    if (o.length && (s.style.position = getComputedStyle(o[0].element).position === "fixed" ? "fixed" : "absolute", s.appendChild(
      x(
        "ul",
        { class: "element-list" },
        o.map((i) => {
          let h = i.menu?.filter(
            ($) => !$.selectors || $.selectors.some((k) => i.selectors.includes(k))
          ), b = o.length > 1 && !!h?.length, y = b ? {
            class: "selectable",
            onClick: /* @__PURE__ */ d(() => E.set(i), "onClick"),
            onMouseEnter: /* @__PURE__ */ d(() => w.set(i), "onMouseEnter"),
            onMouseLeave: /* @__PURE__ */ d(() => w.set(void 0), "onMouseLeave")
          } : t ? { class: "selected", onClick: /* @__PURE__ */ d(() => E.set(void 0), "onClick") } : {}, L = b || t;
          return x("li", y, [
            x(L ? "button" : "div", L ? { type: "button" } : {}, [
              t ? fe() : null,
              x("code", {}, [i.element.outerHTML]),
              b ? be() : null
            ])
          ]);
        })
      )
    )), E.get() || m.get().length === 1) {
      let i = E.get() || m.get()[0], h = i.menu?.filter(
        (b) => !b.selectors || b.selectors.some((y) => i.selectors.includes(y))
      );
      h?.length && s.appendChild(
        x(
          "ul",
          { class: "menu-list" },
          h.map(({ id: b, title: y, description: L, clickEvent: $ }) => {
            let k = $ && (() => n.emit($, b, ee(i)));
            return x("li", {}, [
              x(
                k ? "button" : "div",
                k ? { type: "button", onClick: k } : {},
                [
                  x("div", {}, [
                    x("strong", {}, [y]),
                    L && x("small", {}, [L])
                  ])
                ]
              )
            ]);
          })
        )
      );
    }
    let a = p.get();
    a ? (Object.assign(s.style, {
      display: "block",
      left: `${s.style.position === "fixed" ? a.x - window.scrollX : a.x}px`,
      top: `${s.style.position === "fixed" ? a.y - window.scrollY : a.y}px`
    }), X(s), requestAnimationFrame(() => J(s, a, { topOffset: 15, centered: !0 }))) : (Q(s), Object.assign(s.style, { display: "none" }));
  }, "renderMenu");
  m.subscribe(Y), E.subscribe(Y);
  let se = /* @__PURE__ */ d((s) => {
    let t = K(s);
    l.set((o) => {
      let a = t.id ? o.filter((i) => i.id !== t.id) : o;
      return t.selectors?.length ? [...a, t] : a;
    });
  }, "addHighlight"), F = /* @__PURE__ */ d((s) => {
    l.set((t) => t.filter((o) => o.id !== s));
  }, "removeHighlight"), V = /* @__PURE__ */ d(() => {
    l.set([]), c.set(/* @__PURE__ */ new Map()), r.set([]), p.set(void 0), u.set(void 0), m.set([]), g.set([]), w.set(void 0), E.set(void 0);
  }, "resetState"), q, ie = /* @__PURE__ */ d((s, t) => {
    let o = "scrollIntoView-highlight";
    clearTimeout(q), F(o);
    let a = e.querySelector(s);
    if (!a) {
      console.warn(`Cannot scroll into view: ${s} not found`);
      return;
    }
    a.scrollIntoView({ behavior: "smooth", block: "center", ...t });
    let i = `kf-${Math.random().toString(36).substring(2, 15)}`;
    l.set((h) => [
      ...h,
      {
        id: o,
        priority: 1e3,
        selectors: [s],
        styles: {
          outline: "2px solid #1EA7FD",
          outlineOffset: "-1px",
          animation: `${i} 3s linear forwards`
        },
        keyframes: `@keyframes ${i} {
          0% { outline: 2px solid #1EA7FD; }
          20% { outline: 2px solid #1EA7FD00; }
          40% { outline: 2px solid #1EA7FD; }
          60% { outline: 2px solid #1EA7FD00; }
          80% { outline: 2px solid #1EA7FD; }
          100% { outline: 2px solid #1EA7FD00; }
        }`
      }
    ]), q = setTimeout(() => F(o), 3500);
  }, "scrollIntoView"), re = /* @__PURE__ */ d((s) => {
    requestAnimationFrame(() => u.set({ x: s.pageX, y: s.pageY }));
  }, "onMouseMove");
  e.body.addEventListener("mousemove", re), n.on(z, se), n.on(j, F), n.on(Z, V), n.on(W, ie), n.on(oe.STORY_RENDER_PHASE_CHANGED, ({ newPhase: s }) => {
    s === "loading" && V();
  });
}, "useHighlights");

// src/highlight/preview.ts
globalThis?.FEATURES?.highlight && B.addons?.ready && B.addons.ready().then(ne);
var ye = /* @__PURE__ */ d(() => (0, B.definePreview)({}), "default");
