var ie = Object.defineProperty;
var l = (i, o) => ie(i, "name", { value: o, configurable: !0 });

// src/highlight/preview.ts
import { addons as te, definePreview as ue } from "storybook/preview-api";

// src/highlight/useHighlights.ts
import { STORY_RENDER_PHASE_CHANGED as ce } from "storybook/internal/core-events";

// src/highlight/constants.ts
var B = "storybook/highlight", V = `${B}/add`, q = `${B}/remove`, z = `${B}/reset`, j = `${B}/scroll-into-view`, D = 2147483647;

// src/highlight/utils.ts
var re = "svg,path,rect,circle,line,polyline,polygon,ellipse,text".split(","), x = /* @__PURE__ */ l((i, o, d) => {
  let c = re.includes(i) ? document.createElementNS("http://www.w3.org/2000/svg", i) : document.createElement(i);
  return Object.entries(o).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);
  }), d?.forEach((r) => {
    if (!(r == null || r === !1))
      try {
        c.appendChild(r);
      } catch {
        c.appendChild(document.createTextNode(String(r)));
      }
  }), c;
}, "createElement"), Z = /* @__PURE__ */ l((i) => {
  if ("elements" in i) {
    let { elements: o, color: d, style: c } = i;
    return {
      selectors: o,
      styles: {
        outline: `2px ${c} ${d}`,
        outlineOffset: "2px",
        boxShadow: "0 0 0 6px rgba(255,255,255,0.6)"
      }
    };
  }
  return {
    styles: {
      outline: "2px dashed #029cfd"
    },
    ...i
  };
}, "convertLegacy"), le = /* @__PURE__ */ l((i) => i instanceof Function, "isFunction"), S = /* @__PURE__ */ new Map(), I = /* @__PURE__ */ new Map(),
A = /* @__PURE__ */ new Map(), T = /* @__PURE__ */ l((i) => {
  let o = Symbol();
  return I.set(o, []), S.set(o, i), { get: /* @__PURE__ */ l(() => S.get(o), "get"), set: /* @__PURE__ */ l((u) => {
    let m = S.get(o), g = le(u) ? u(m) : u;
    g !== m && (S.set(o, g), I.get(o)?.forEach((w) => {
      A.get(w)?.(), A.set(w, w(g));
    }));
  }, "set"), subscribe: /* @__PURE__ */ l((u) => (I.get(o)?.push(u), () => {
    let m = I.get(o);
    m && I.set(
      o,
      m.filter((g) => g !== u)
    );
  }), "subscribe"), teardown: /* @__PURE__ */ l(() => {
    I.get(o)?.forEach((u) => {
      A.get(u)?.(), A.delete(u);
    }), I.delete(o), S.delete(o);
  }, "teardown") };
}, "useStore"), F = /* @__PURE__ */ l((i) => {
  let o = document.getElementById("storybook-root"), d = /* @__PURE__ */ new Map();
  for (let c of i) {
    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.
        ...o?.querySelectorAll(p) || []
      ];
      for (let m of u) {
        let g = d.get(m);
        (!g || g.priority <= r) && d.set(m, {
          ...c,
          priority: r,
          selectors: Array.from(new Set((g?.selectors || []).concat(p)))
        });
      }
    }
  }
  return d;
}, "mapElements"), W = /* @__PURE__ */ l((i) => Array.from(i.entries()).map(([o, { selectors: d, styles: c, hoverStyles: r, focusStyles: p, menu: u }]) => {
  let { top: m, left: g, width: w, height: E } = o.getBoundingClientRect(), { position: v } = getComputedStyle(o);
  return {
    element: o,
    selectors: d,
    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((o, d) => d.width * d.height - o.width * o.height), "mapBoxes"), _ = /* @__PURE__ */ l((i, o) => {
  let d = i.getBoundingClientRect(), { x: c, y: r } = o;
  return d?.top && d?.left && c >= d.left && c <= d.left + d.width && r >= d.top && r <= d.top + d.height;
}, "isOverMenu"), N = /* @__PURE__ */ l((i, o, d) => {
  if (!o || !d)
    return !1;
  let { left: c, top: r, width: p, height: u } = i;
  (u === 0 || p === 0) && (c -= 10, r -= 10, p += 20, u += 20), o.style.position === "fixed" && (c += window.scrollX, r += window.scrollY);
  let { x: m, y: g } = d;
  return m >= c && m <= c + p && g >= r && g <= r + u;
}, "isTargeted"), K = /* @__PURE__ */ l((i, o, d = {}) => {
  let { x: c, y: r } = o, { margin: p = 5, topOffset: u = 0, centered: m = !1 } = d, { scrollX: g, scrollY: w, innerHeight: E, innerWidth: v } = window,
  M = Math.min(
    i.style.position === "fixed" ? r - w : r,
    E - i.clientHeight - p - u + w
  ), H = m ? i.clientWidth / 2 : 0, C = i.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(i.style, {
    ...C !== c && { left: `${C}px` },
    ...M !== r && { top: `${M}px` }
  });
}, "keepInViewport"), G = /* @__PURE__ */ l((i) => {
  window.HTMLElement.prototype.hasOwnProperty("showPopover") && i.showPopover();
}, "showPopover"), U = /* @__PURE__ */ l((i) => {
  window.HTMLElement.prototype.hasOwnProperty("showPopover") && i.hidePopover();
}, "hidePopover"), J = /* @__PURE__ */ l((i) => ({
  top: i.top,
  left: i.left,
  width: i.width,
  height: i.height,
  selectors: i.selectors,
  element: {
    attributes: Object.fromEntries(
      Array.from(i.element.attributes).map((o) => [o.name, o.value])
    ),
    localName: i.element.localName,
    tagName: i.element.tagName,
    outerHTML: i.element.outerHTML
  }
}), "getEventDetails");

// src/highlight/useHighlights.ts
var f = "storybook-highlights-menu", Q = "storybook-highlights-root", de = "storybook-root", ae = /* @__PURE__ */ l(() => 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"), pe = /* @__PURE__ */ l(() => 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"), ee = /* @__PURE__ */ l((i) => {
  if (globalThis.__STORYBOOK_HIGHLIGHT_INITIALIZED)
    return;
  globalThis.__STORYBOOK_HIGHLIGHT_INITIALIZED = !0;
  let { document: o } = globalThis, d = T([]), c = T(/* @__PURE__ */ new Map()), r = T([]), p = T(), u = T(), m = T([]), g = T([]), w = T(),
  E = T(), v = o.getElementById(Q);
  d.subscribe(() => {
    v || (v = x("div", { id: Q }), o.body.appendChild(v));
  }), d.subscribe((n) => {
    let e = o.getElementById(de);
    if (!e)
      return;
    c.set(F(n));
    let t = new MutationObserver(() => c.set(F(n)));
    return t.observe(e, { subtree: !0, childList: !0 }), () => {
      t.disconnect();
    };
  }), c.subscribe((n) => {
    let e = /* @__PURE__ */ l(() => requestAnimationFrame(() => r.set(W(n))), "updateBoxes"), t = new ResizeObserver(e);
    t.observe(o.body), Array.from(n.keys()).forEach((s) => t.observe(s));
    let a = Array.from(o.body.querySelectorAll("*")).filter((s) => {
      let { overflow: h, overflowX: b, overflowY: y } = window.getComputedStyle(s);
      return ["auto", "scroll"].some((L) => [h, b, y].includes(L));
    });
    return a.forEach((s) => s.addEventListener("scroll", e)), () => {
      t.disconnect(), a.forEach((s) => s.removeEventListener("scroll", e));
    };
  }), c.subscribe((n) => {
    let e = Array.from(n.keys()).filter(({ style: a }) => a.position === "sticky"), t = /* @__PURE__ */ l(() => requestAnimationFrame(() => {
      r.set(
        (a) => a.map((s) => {
          if (e.includes(s.element)) {
            let { top: h, left: b } = s.element.getBoundingClientRect();
            return { ...s, top: h + window.scrollY, left: b + window.scrollX };
          }
          return s;
        })
      );
    }), "updateBoxes");
    return o.addEventListener("scroll", t), () => o.removeEventListener("scroll", t);
  }), c.subscribe((n) => {
    m.set((e) => e.filter(({ element: t }) => n.has(t)));
  }), m.subscribe((n) => {
    n.length ? (E.set((e) => n.some((t) => t.element === e?.element) ? e : void 0), w.set((e) => n.some((t) => t.element === e?.element) ? e :
    void 0)) : (E.set(void 0), w.set(void 0), p.set(void 0));
  });
  let M = new Map(/* @__PURE__ */ new Map());
  d.subscribe((n) => {
    n.forEach(({ keyframes: e }) => {
      if (e) {
        let t = M.get(e);
        t || (t = o.createElement("style"), t.setAttribute("data-highlight", "keyframes"), M.set(e, t), o.head.appendChild(t)), t.innerHTML =
        e;
      }
    }), M.forEach((e, t) => {
      n.some((a) => a.keyframes === t) || (e.remove(), M.delete(t));
    });
  });
  let H = new Map(/* @__PURE__ */ new Map());
  r.subscribe((n) => {
    n.forEach((e) => {
      let t = H.get(e.element);
      if (v && !t) {
        let a = {
          popover: "manual",
          "data-highlight-dimensions": `w${e.width.toFixed(0)}h${e.height.toFixed(0)}`,
          "data-highlight-coordinates": `x${e.left.toFixed(0)}y${e.top.toFixed(0)}`
        };
        t = v.appendChild(x("div", a)), H.set(e.element, t);
      }
    }), H.forEach((e, t) => {
      n.some(({ element: a }) => a === t) || (e.remove(), H.delete(t));
    });
  }), r.subscribe((n) => {
    let e = n.filter((a) => a.menu);
    if (!e.length)
      return;
    let t = /* @__PURE__ */ l((a) => {
      requestAnimationFrame(() => {
        let s = o.getElementById(f), h = { x: a.pageX, y: a.pageY };
        if (s && !_(s, h)) {
          let b = e.filter((y) => {
            let L = H.get(y.element);
            return N(y, L, h);
          });
          p.set(b.length ? h : void 0), m.set(b);
        }
      });
    }, "onClick");
    return o.addEventListener("click", t), () => o.removeEventListener("click", t);
  });
  let C = /* @__PURE__ */ l(() => {
    let n = o.getElementById(f), e = u.get();
    !e || n && _(n, e) || g.set((t) => {
      let a = r.get().filter((y) => {
        let L = H.get(y.element);
        return N(y, L, e);
      }), s = t.filter((y) => a.includes(y)), h = a.filter((y) => !t.includes(y)), b = t.length - s.length;
      return h.length || b ? [...s, ...h] : t;
    });
  }, "updateHovered");
  u.subscribe(C), r.subscribe(C);
  let O = /* @__PURE__ */ l(() => {
    let n = E.get(), e = n ? [n] : m.get(), t = e.length === 1 ? e[0] : w.get(), a = p.get() !== void 0;
    r.get().forEach((s) => {
      let h = H.get(s.element);
      if (h) {
        let b = t === s, y = a ? t ? b : e.includes(s) : g.get()?.includes(s);
        Object.assign(h.style, {
          animation: "none",
          background: "transparent",
          border: "none",
          boxSizing: "border-box",
          outline: "none",
          outlineOffset: "0px",
          ...s.styles,
          ...y ? s.hoverStyles : {},
          ...b ? s.focusStyles : {},
          position: getComputedStyle(s.element).position === "fixed" ? "fixed" : "absolute",
          zIndex: D - 10,
          top: `${s.top}px`,
          left: `${s.left}px`,
          width: `${s.width}px`,
          height: `${s.height}px`,
          margin: 0,
          padding: 0,
          cursor: s.menu ? "pointer" : "default",
          pointerEvents: s.menu ? "auto" : "none"
        }), G(h);
      }
    });
  }, "updateBoxStyles");
  r.subscribe(O), m.subscribe(O), g.subscribe(O), w.subscribe(O), E.subscribe(O);
  let P = /* @__PURE__ */ l(() => {
    if (!v)
      return;
    let n = o.getElementById(f);
    if (n)
      n.innerHTML = "";
    else {
      let s = { id: f, popover: "manual" };
      n = v.appendChild(x("div", s)), v.appendChild(
        x("style", {}, [
          `
            #${f} {
              position: absolute;
              z-index: ${D};
              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 e = E.get(), t = e ? [e] : m.get();
    if (t.length && (n.style.position = getComputedStyle(t[0].element).position === "fixed" ? "fixed" : "absolute", n.appendChild(
      x(
        "ul",
        { class: "element-list" },
        t.map((s) => {
          let h = s.menu?.filter(
            ($) => !$.selectors || $.selectors.some((k) => s.selectors.includes(k))
          ), b = t.length > 1 && !!h?.length, y = b ? {
            class: "selectable",
            onClick: /* @__PURE__ */ l(() => E.set(s), "onClick"),
            onMouseEnter: /* @__PURE__ */ l(() => w.set(s), "onMouseEnter"),
            onMouseLeave: /* @__PURE__ */ l(() => w.set(void 0), "onMouseLeave")
          } : e ? { class: "selected", onClick: /* @__PURE__ */ l(() => E.set(void 0), "onClick") } : {}, L = b || e;
          return x("li", y, [
            x(L ? "button" : "div", L ? { type: "button" } : {}, [
              e ? ae() : null,
              x("code", {}, [s.element.outerHTML]),
              b ? pe() : null
            ])
          ]);
        })
      )
    )), E.get() || m.get().length === 1) {
      let s = E.get() || m.get()[0], h = s.menu?.filter(
        (b) => !b.selectors || b.selectors.some((y) => s.selectors.includes(y))
      );
      h?.length && n.appendChild(
        x(
          "ul",
          { class: "menu-list" },
          h.map(({ id: b, title: y, description: L, clickEvent: $ }) => {
            let k = $ && (() => i.emit($, b, J(s)));
            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(n.style, {
      display: "block",
      left: `${n.style.position === "fixed" ? a.x - window.scrollX : a.x}px`,
      top: `${n.style.position === "fixed" ? a.y - window.scrollY : a.y}px`
    }), G(n), requestAnimationFrame(() => K(n, a, { topOffset: 15, centered: !0 }))) : (U(n), Object.assign(n.style, { display: "none" }));
  }, "renderMenu");
  m.subscribe(P), E.subscribe(P);
  let oe = /* @__PURE__ */ l((n) => {
    let e = Z(n);
    d.set((t) => {
      let a = e.id ? t.filter((s) => s.id !== e.id) : t;
      return e.selectors?.length ? [...a, e] : a;
    });
  }, "addHighlight"), R = /* @__PURE__ */ l((n) => {
    d.set((e) => e.filter((t) => t.id !== n));
  }, "removeHighlight"), X = /* @__PURE__ */ l(() => {
    d.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"), Y, ne = /* @__PURE__ */ l((n, e) => {
    let t = "scrollIntoView-highlight";
    clearTimeout(Y), R(t);
    let a = o.querySelector(n);
    if (!a) {
      console.warn(`Cannot scroll into view: ${n} not found`);
      return;
    }
    a.scrollIntoView({ behavior: "smooth", block: "center", ...e });
    let s = `kf-${Math.random().toString(36).substring(2, 15)}`;
    d.set((h) => [
      ...h,
      {
        id: t,
        priority: 1e3,
        selectors: [n],
        styles: {
          outline: "2px solid #1EA7FD",
          outlineOffset: "-1px",
          animation: `${s} 3s linear forwards`
        },
        keyframes: `@keyframes ${s} {
          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; }
        }`
      }
    ]), Y = setTimeout(() => R(t), 3500);
  }, "scrollIntoView"), se = /* @__PURE__ */ l((n) => {
    requestAnimationFrame(() => u.set({ x: n.pageX, y: n.pageY }));
  }, "onMouseMove");
  o.body.addEventListener("mousemove", se), i.on(V, oe), i.on(q, R), i.on(z, X), i.on(j, ne), i.on(ce, ({ newPhase: n }) => {
    n === "loading" && X();
  });
}, "useHighlights");

// src/highlight/preview.ts
globalThis?.FEATURES?.highlight && te?.ready && te.ready().then(ee);
var Le = /* @__PURE__ */ l(() => ue({}), "default");
export {
  Le as default
};
