var ae = Object.defineProperty;
var l = (s, e) => ae(s, "name", { value: e, configurable: !0 });

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

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

// src/highlight/constants.ts
var B = "storybook/highlight", W = `${B}/add`, K = `${B}/remove`, U = `${B}/reset`, J = `${B}/scroll-into-view`, N = 2147483647;

// src/highlight/icons.ts
var _ = {
  chevronLeft: [
    "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.7011\
8 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.65829 9.\
10355 3.85355L5.95711 7L9.10355 10.1464Z"
  ],
  chevronRight: [
    "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.2988\
2 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.65829 4.\
89645 3.85355L8.04289 7L4.89645 10.1464Z"
  ],
  info: [
    "M7 5.5a.5.5 0 01.5.5v4a.5.5 0 01-1 0V6a.5.5 0 01.5-.5zM7 4.5A.75.75 0 107 3a.75.75 0 000 1.5z",
    "M7 14A7 7 0 107 0a7 7 0 000 14zm0-1A6 6 0 107 1a6 6 0 000 12z"
  ],
  shareAlt: [
    "M2 1.004a1 1 0 00-1 1v10a1 1 0 001 1h10a1 1 0 001-1v-4.5a.5.5 0 00-1 0v4.5H2v-10h4.5a.5.5 0 000-1H2z",
    "M7.354 7.357L12 2.711v1.793a.5.5 0 001 0v-3a.5.5 0 00-.5-.5h-3a.5.5 0 100 1h1.793L6.646 6.65a.5.5 0 10.708.707z"
  ]
};

// src/highlight/utils.ts
var pe = "svg,path,rect,circle,line,polyline,polygon,ellipse,text".split(","), b = /* @__PURE__ */ l((s, e, d) => {
  let c = pe.includes(s) ? document.createElementNS("http://www.w3.org/2000/svg", s) : document.createElement(s);
  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);
  }), d?.forEach((r) => {
    if (!(r == null || r === !1))
      try {
        c.appendChild(r);
      } catch {
        c.appendChild(document.createTextNode(String(r)));
      }
  }), c;
}, "createElement"), A = /* @__PURE__ */ l((s) => _[s] && b(
  "svg",
  { width: "14", height: "14", viewBox: "0 0 14 14", xmlns: "http://www.w3.org/2000/svg" },
  _[s].map(
    (e) => b("path", {
      fill: "currentColor",
      "fill-rule": "evenodd",
      "clip-rule": "evenodd",
      d: e
    })
  )
), "createIcon"), Q = /* @__PURE__ */ l((s) => {
  if ("elements" in s) {
    let { elements: c, color: r, style: p } = s;
    return {
      id: void 0,
      priority: 0,
      selectors: c,
      styles: {
        outline: `2px ${p} ${r}`,
        outlineOffset: "2px",
        boxShadow: "0 0 0 6px rgba(255,255,255,0.6)"
      },
      menu: void 0
    };
  }
  let { menu: e, ...d } = s;
  return {
    id: void 0,
    priority: 0,
    styles: {
      outline: "2px dashed #029cfd"
    },
    ...d,
    menu: Array.isArray(e) ? e.every(Array.isArray) ? e : [e] : void 0
  };
}, "normalizeOptions"), ue = /* @__PURE__ */ l((s) => s instanceof Function, "isFunction"), k = /* @__PURE__ */ new Map(), I = /* @__PURE__ */ new Map(),
R = /* @__PURE__ */ new Map(), M = /* @__PURE__ */ l((s) => {
  let e = Symbol();
  return I.set(e, []), k.set(e, s), { get: /* @__PURE__ */ l(() => k.get(e), "get"), set: /* @__PURE__ */ l((u) => {
    let m = k.get(e), h = ue(u) ? u(m) : u;
    h !== m && (k.set(e, h), I.get(e)?.forEach((w) => {
      R.get(w)?.(), R.set(w, w(h));
    }));
  }, "set"), subscribe: /* @__PURE__ */ l((u) => (I.get(e)?.push(u), () => {
    let m = I.get(e);
    m && I.set(
      e,
      m.filter((h) => h !== u)
    );
  }), "subscribe"), teardown: /* @__PURE__ */ l(() => {
    I.get(e)?.forEach((u) => {
      R.get(u)?.(), R.delete(u);
    }), I.delete(e), k.delete(e);
  }, "teardown") };
}, "useStore"), G = /* @__PURE__ */ l((s) => {
  let e = document.getElementById("storybook-root"), d = /* @__PURE__ */ new Map();
  for (let c of s) {
    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 h = d.get(m);
        (!h || h.priority <= r) && d.set(m, {
          ...c,
          priority: r,
          selectors: Array.from(new Set((h?.selectors || []).concat(p)))
        });
      }
    }
  }
  return d;
}, "mapElements"), ee = /* @__PURE__ */ l((s) => Array.from(s.entries()).map(([e, { selectors: d, styles: c, hoverStyles: r, focusStyles: p,
menu: u }]) => {
  let { top: m, left: h, width: w, height: E } = e.getBoundingClientRect(), { position: v } = getComputedStyle(e);
  return {
    element: e,
    selectors: d,
    styles: c,
    hoverStyles: r,
    focusStyles: p,
    menu: u,
    top: v === "fixed" ? m : m + window.scrollY,
    left: v === "fixed" ? h : h + window.scrollX,
    width: w,
    height: E
  };
}).sort((e, d) => d.width * d.height - e.width * e.height), "mapBoxes"), P = /* @__PURE__ */ l((s, e) => {
  let d = s.getBoundingClientRect(), { x: c, y: r } = e;
  return d?.top && d?.left && c >= d.left && c <= d.left + d.width && r >= d.top && r <= d.top + d.height;
}, "isOverMenu"), z = /* @__PURE__ */ l((s, e, d) => {
  if (!e || !d)
    return !1;
  let { left: c, top: r, width: p, height: u } = s;
  (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: h } = d;
  return m >= c && m <= c + p && h >= r && h <= r + u;
}, "isTargeted"), te = /* @__PURE__ */ l((s, e, d = {}) => {
  let { x: c, y: r } = e, { margin: p = 5, topOffset: u = 0, centered: m = !1 } = d, { scrollX: h, scrollY: w, innerHeight: E, innerWidth: v } = window,
  T = Math.min(
    s.style.position === "fixed" ? r - w : r,
    E - s.clientHeight - p - u + w
  ), H = m ? s.clientWidth / 2 : 0, O = s.style.position === "fixed" ? Math.max(Math.min(c - h, v - H - p), H + p) : Math.max(
    Math.min(c, v - H - p + h),
    H + p + h
  );
  Object.assign(s.style, {
    ...O !== c && { left: `${O}px` },
    ...T !== r && { top: `${T}px` }
  });
}, "keepInViewport"), X = /* @__PURE__ */ l((s) => {
  window.HTMLElement.prototype.hasOwnProperty("showPopover") && s.showPopover();
}, "showPopover"), oe = /* @__PURE__ */ l((s) => {
  window.HTMLElement.prototype.hasOwnProperty("showPopover") && s.hidePopover();
}, "hidePopover"), ne = /* @__PURE__ */ l((s) => ({
  top: s.top,
  left: s.left,
  width: s.width,
  height: s.height,
  selectors: s.selectors,
  element: {
    attributes: Object.fromEntries(
      Array.from(s.element.attributes).map((e) => [e.name, e.value])
    ),
    localName: s.element.localName,
    tagName: s.element.tagName,
    outerHTML: s.element.outerHTML
  }
}), "getEventDetails");

// src/highlight/useHighlights.ts
var g = "storybook-highlights-menu", se = "storybook-highlights-root", ge = "storybook-root", ie = /* @__PURE__ */ l((s) => {
  if (globalThis.__STORYBOOK_HIGHLIGHT_INITIALIZED)
    return;
  globalThis.__STORYBOOK_HIGHLIGHT_INITIALIZED = !0;
  let { document: e } = globalThis, d = M([]), c = M(/* @__PURE__ */ new Map()), r = M([]), p = M(), u = M(), m = M([]), h = M([]), w = M(),
  E = M(), v = e.getElementById(se);
  d.subscribe(() => {
    v || (v = b("div", { id: se }), e.body.appendChild(v));
  }), d.subscribe((n) => {
    let t = e.getElementById(ge);
    if (!t)
      return;
    c.set(G(n));
    let o = new MutationObserver(() => c.set(G(n)));
    return o.observe(t, { subtree: !0, childList: !0 }), () => {
      o.disconnect();
    };
  }), c.subscribe((n) => {
    let t = /* @__PURE__ */ l(() => requestAnimationFrame(() => r.set(ee(n))), "updateBoxes"), o = new ResizeObserver(t);
    o.observe(e.body), Array.from(n.keys()).forEach((i) => o.observe(i));
    let a = Array.from(e.body.querySelectorAll("*")).filter((i) => {
      let { overflow: f, overflowX: x, overflowY: y } = window.getComputedStyle(i);
      return ["auto", "scroll"].some((L) => [f, x, y].includes(L));
    });
    return a.forEach((i) => i.addEventListener("scroll", t)), () => {
      o.disconnect(), a.forEach((i) => i.removeEventListener("scroll", t));
    };
  }), c.subscribe((n) => {
    let t = Array.from(n.keys()).filter(({ style: a }) => a.position === "sticky"), o = /* @__PURE__ */ l(() => requestAnimationFrame(() => {
      r.set(
        (a) => a.map((i) => {
          if (t.includes(i.element)) {
            let { top: f, left: x } = i.element.getBoundingClientRect();
            return { ...i, top: f + window.scrollY, left: x + window.scrollX };
          }
          return i;
        })
      );
    }), "updateBoxes");
    return e.addEventListener("scroll", o), () => e.removeEventListener("scroll", o);
  }), c.subscribe((n) => {
    m.set((t) => t.filter(({ element: o }) => n.has(o)));
  }), m.subscribe((n) => {
    n.length ? (E.set((t) => n.some((o) => o.element === t?.element) ? t : void 0), w.set((t) => n.some((o) => o.element === t?.element) ? t :
    void 0)) : (E.set(void 0), w.set(void 0), p.set(void 0));
  });
  let T = new Map(/* @__PURE__ */ new Map());
  d.subscribe((n) => {
    n.forEach(({ keyframes: t }) => {
      if (t) {
        let o = T.get(t);
        o || (o = e.createElement("style"), o.setAttribute("data-highlight", "keyframes"), T.set(t, o), e.head.appendChild(o)), o.innerHTML =
        t;
      }
    }), T.forEach((t, o) => {
      n.some((a) => a.keyframes === o) || (t.remove(), T.delete(o));
    });
  });
  let H = new Map(/* @__PURE__ */ new Map());
  r.subscribe((n) => {
    n.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(b("div", a)), H.set(t.element, o);
      }
    }), H.forEach((t, o) => {
      n.some(({ element: a }) => a === o) || (t.remove(), H.delete(o));
    });
  }), r.subscribe((n) => {
    let t = n.filter((a) => a.menu);
    if (!t.length)
      return;
    let o = /* @__PURE__ */ l((a) => {
      requestAnimationFrame(() => {
        let i = e.getElementById(g), f = { x: a.pageX, y: a.pageY };
        if (i && !P(i, f)) {
          let x = t.filter((y) => {
            let L = H.get(y.element);
            return z(y, L, f);
          });
          p.set(x.length ? f : void 0), m.set(x);
        }
      });
    }, "onClick");
    return e.addEventListener("click", o), () => e.removeEventListener("click", o);
  });
  let O = /* @__PURE__ */ l(() => {
    let n = e.getElementById(g), t = u.get();
    !t || n && P(n, t) || h.set((o) => {
      let a = r.get().filter((y) => {
        let L = H.get(y.element);
        return z(y, L, t);
      }), i = o.filter((y) => a.includes(y)), f = a.filter((y) => !o.includes(y)), x = o.length - i.length;
      return f.length || x ? [...i, ...f] : o;
    });
  }, "updateHovered");
  u.subscribe(O), r.subscribe(O);
  let $ = /* @__PURE__ */ l(() => {
    let n = E.get(), t = n ? [n] : m.get(), o = t.length === 1 ? t[0] : w.get(), a = p.get() !== void 0;
    r.get().forEach((i) => {
      let f = H.get(i.element);
      if (f) {
        let x = o === i, y = a ? o ? x : t.includes(i) : h.get()?.includes(i);
        Object.assign(f.style, {
          animation: "none",
          background: "transparent",
          border: "none",
          boxSizing: "border-box",
          outline: "none",
          outlineOffset: "0px",
          ...i.styles,
          ...y ? i.hoverStyles : {},
          ...x ? i.focusStyles : {},
          position: getComputedStyle(i.element).position === "fixed" ? "fixed" : "absolute",
          zIndex: N - 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(f);
      }
    });
  }, "updateBoxStyles");
  r.subscribe($), m.subscribe($), h.subscribe($), w.subscribe($), E.subscribe($);
  let V = /* @__PURE__ */ l(() => {
    if (!v)
      return;
    let n = e.getElementById(g);
    if (n)
      n.innerHTML = "";
    else {
      let i = { id: g, popover: "manual" };
      n = v.appendChild(b("div", i)), v.appendChild(
        b("style", {}, [
          `
            #${g} {
              position: absolute;
              z-index: ${N};
              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;
            }
            #${g} ul {
              list-style: none;
              margin: 0;
              padding: 0;
            }
            #${g} > ul {
              max-height: 300px;
              overflow-y: auto;
              padding: 4px 0;
            }
            #${g} li {
              padding: 0 4px;
              margin: 0;
            }
            #${g} li > :not(ul) {
              display: flex;
              padding: 8px;
              margin: 0;
              align-items: center;
              gap: 8px;
              border-radius: 4px;
            }
            #${g} button {
              width: 100%;
              border: 0;
              background: transparent;
              color: inherit;
              text-align: left;
              font-family: inherit;
              font-size: inherit;
            }
            #${g} button:focus-visible {
              outline-color: #029CFD;
            }
            #${g} button:hover {
              background: rgba(2, 156, 253, 0.07);
              color: #029CFD;
              cursor: pointer;
            }
            #${g} li code {
              white-space: nowrap;
              overflow: hidden;
              text-overflow: ellipsis;
              line-height: 16px;
              font-size: 11px;
            }
            #${g} li svg {
              flex-shrink: 0;
              margin: 1px;
              color: #73828C;
            }
            #${g} li > button:hover svg, #${g} li > button:focus-visible svg {
              color: #029CFD;
            }
            #${g} .element-list li svg {
              display: none;
            }
            #${g} li.selectable svg, #${g} li.selected svg {
              display: block;
            }
            #${g} .menu-list {
              border-top: 1px solid rgba(38, 85, 115, 0.15);
            }
            #${g} .menu-list > li:not(:last-child) {
              padding-bottom: 4px;
              margin-bottom: 4px;
              border-bottom: 1px solid rgba(38, 85, 115, 0.15);
            }
            #${g} .menu-items, #${g} .menu-items li {
              padding: 0;
            }
            #${g} .menu-item {
              display: flex;
            }
            #${g} .menu-item-content {
              display: flex;
              flex-direction: column;
              flex-grow: 1;
            }
          `
        ])
      );
    }
    let t = E.get(), o = t ? [t] : m.get();
    if (o.length && (n.style.position = getComputedStyle(o[0].element).position === "fixed" ? "fixed" : "absolute", n.appendChild(
      b(
        "ul",
        { class: "element-list" },
        o.map((i) => {
          let f = o.length > 1 && !!i.menu?.some(
            (L) => L.some(
              (C) => !C.selectors || C.selectors.some((S) => i.selectors.includes(S))
            )
          ), x = f ? {
            class: "selectable",
            onClick: /* @__PURE__ */ l(() => E.set(i), "onClick"),
            onMouseEnter: /* @__PURE__ */ l(() => w.set(i), "onMouseEnter"),
            onMouseLeave: /* @__PURE__ */ l(() => w.set(void 0), "onMouseLeave")
          } : t ? { class: "selected", onClick: /* @__PURE__ */ l(() => E.set(void 0), "onClick") } : {}, y = f || t;
          return b("li", x, [
            b(y ? "button" : "div", y ? { type: "button" } : {}, [
              t ? A("chevronLeft") : null,
              b("code", {}, [i.element.outerHTML]),
              f ? A("chevronRight") : null
            ])
          ]);
        })
      )
    )), E.get() || m.get().length === 1) {
      let i = E.get() || m.get()[0], f = i.menu?.filter(
        (x) => x.some(
          (y) => !y.selectors || y.selectors.some((L) => i.selectors.includes(L))
        )
      );
      f?.length && n.appendChild(
        b(
          "ul",
          { class: "menu-list" },
          f.map(
            (x) => b("li", {}, [
              b(
                "ul",
                { class: "menu-items" },
                x.map(
                  ({ id: y, title: L, description: C, iconLeft: S, iconRight: j, clickEvent: Z }) => {
                    let F = Z && (() => s.emit(Z, y, ne(i)));
                    return b("li", {}, [
                      b(
                        F ? "button" : "div",
                        F ? { class: "menu-item", type: "button", onClick: F } : { class: "menu-item" },
                        [
                          S ? A(S) : null,
                          b("div", { class: "menu-item-content" }, [
                            b(C ? "strong" : "span", {}, [L]),
                            C && b("span", {}, [C])
                          ]),
                          j ? A(j) : null
                        ]
                      )
                    ]);
                  }
                )
              )
            ])
          )
        )
      );
    }
    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`
    }), X(n), requestAnimationFrame(() => te(n, a, { topOffset: 15, centered: !0 }))) : (oe(n), Object.assign(n.style, { display: "none" }));
  }, "renderMenu");
  m.subscribe(V), E.subscribe(V);
  let le = /* @__PURE__ */ l((n) => {
    let t = Q(n);
    d.set((o) => {
      let a = t.id ? o.filter((i) => i.id !== t.id) : o;
      return t.selectors?.length ? [...a, t] : a;
    });
  }, "addHighlight"), D = /* @__PURE__ */ l((n) => {
    n && d.set((t) => t.filter((o) => o.id !== n));
  }, "removeHighlight"), Y = /* @__PURE__ */ l(() => {
    d.set([]), c.set(/* @__PURE__ */ new Map()), r.set([]), p.set(void 0), u.set(void 0), m.set([]), h.set([]), w.set(void 0), E.set(void 0);
  }, "resetState"), q, ce = /* @__PURE__ */ l((n, t) => {
    let o = "scrollIntoView-highlight";
    clearTimeout(q), D(o);
    let a = e.querySelector(n);
    if (!a) {
      console.warn(`Cannot scroll into view: ${n} not found`);
      return;
    }
    a.scrollIntoView({ behavior: "smooth", block: "center", ...t });
    let i = `kf-${Math.random().toString(36).substring(2, 15)}`;
    d.set((f) => [
      ...f,
      {
        id: o,
        priority: 1e3,
        selectors: [n],
        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(() => D(o), 3500);
  }, "scrollIntoView"), de = /* @__PURE__ */ l((n) => {
    requestAnimationFrame(() => u.set({ x: n.pageX, y: n.pageY }));
  }, "onMouseMove");
  e.body.addEventListener("mousemove", de), s.on(W, le), s.on(K, D), s.on(U, Y), s.on(J, ce), s.on(me, ({ newPhase: n }) => {
    n === "loading" && Y();
  });
}, "useHighlights");

// src/highlight/preview.ts
globalThis?.FEATURES?.highlight && re?.ready && re.ready().then(ie);
var Oe = /* @__PURE__ */ l(() => he({}), "default");
export {
  Oe as default
};
