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

// src/highlight/preview.ts
import { definePreviewAddon as fe } from "storybook/internal/csf";
import { addons as le } from "storybook/preview-api";

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

// src/highlight/constants.ts
var _ = "storybook/highlight", U = `${_}/add`, K = `${_}/remove`, J = `${_}/reset`, Q = `${_}/scroll-into-view`, F = 2147483647, L = 28;

// src/highlight/icons.ts
var G = {
  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 ue = "svg,path,rect,circle,line,polyline,polygon,ellipse,text".split(","), b = /* @__PURE__ */ c((s, e = {}, d) => {
  let l = ue.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" && (l.addEventListener("click", p), l.addEventListener("keydown", (u) => {
      (u.key === "Enter" || u.key === " ") && (u.preventDefault(), p());
    })), r === "onMouseEnter" && l.addEventListener("mouseenter", p), r === "onMouseLeave" && l.addEventListener("mouseleave", p)) : l.setAttribute(
    r, p);
  }), d?.forEach((r) => {
    if (!(r == null || r === !1))
      try {
        l.appendChild(r);
      } catch {
        l.appendChild(document.createTextNode(String(r)));
      }
  }), l;
}, "createElement"), k = /* @__PURE__ */ c((s) => G[s] && b(
  "svg",
  { width: "14", height: "14", viewBox: "0 0 14 14", xmlns: "http://www.w3.org/2000/svg" },
  G[s].map(
    (e) => b("path", {
      fill: "currentColor",
      "fill-rule": "evenodd",
      "clip-rule": "evenodd",
      d: e
    })
  )
), "createIcon"), ee = /* @__PURE__ */ c((s) => {
  if ("elements" in s) {
    let { elements: l, color: r, style: p } = s;
    return {
      id: void 0,
      priority: 0,
      selectors: l,
      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"), me = /* @__PURE__ */ c((s) => s instanceof Function, "isFunction"), S = /* @__PURE__ */ new Map(), C = /* @__PURE__ */ new Map(),
R = /* @__PURE__ */ new Map(), T = /* @__PURE__ */ c((s) => {
  let e = Symbol();
  return C.set(e, []), S.set(e, s), { get: /* @__PURE__ */ c(() => S.get(e), "get"), set: /* @__PURE__ */ c((u) => {
    let m = S.get(e), f = me(u) ? u(m) : u;
    f !== m && (S.set(e, f), C.get(e)?.forEach((E) => {
      R.get(E)?.(), R.set(E, E(f));
    }));
  }, "set"), subscribe: /* @__PURE__ */ c((u) => (C.get(e)?.push(u), () => {
    let m = C.get(e);
    m && C.set(
      e,
      m.filter((f) => f !== u)
    );
  }), "subscribe"), teardown: /* @__PURE__ */ c(() => {
    C.get(e)?.forEach((u) => {
      R.get(u)?.(), R.delete(u);
    }), C.delete(e), S.delete(e);
  }, "teardown") };
}, "useStore"), z = /* @__PURE__ */ c((s) => {
  let e = document.getElementById("storybook-root"), d = /* @__PURE__ */ new Map();
  for (let l of s) {
    let { priority: r = 0 } = l;
    for (let p of l.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 f = d.get(m);
        (!f || f.priority <= r) && d.set(m, {
          ...l,
          priority: r,
          selectors: Array.from(new Set((f?.selectors || []).concat(p)))
        });
      }
    }
  }
  return d;
}, "mapElements"), te = /* @__PURE__ */ c((s) => Array.from(s.entries()).map(([e, { selectors: d, styles: l, hoverStyles: r, focusStyles: p,
menu: u }]) => {
  let { top: m, left: f, width: E, height: w } = e.getBoundingClientRect(), { position: v } = getComputedStyle(e);
  return {
    element: e,
    selectors: d,
    styles: l,
    hoverStyles: r,
    focusStyles: p,
    menu: u,
    top: v === "fixed" ? m : m + window.scrollY,
    left: v === "fixed" ? f : f + window.scrollX,
    width: E,
    height: w
  };
}).sort((e, d) => d.width * d.height - e.width * e.height), "mapBoxes"), P = /* @__PURE__ */ c((s, e) => {
  let d = s.getBoundingClientRect(), { x: l, y: r } = e;
  return d?.top && d?.left && l >= d.left && l <= d.left + d.width && r >= d.top && r <= d.top + d.height;
}, "isOverMenu"), X = /* @__PURE__ */ c((s, e, d) => {
  if (!e || !d)
    return !1;
  let { left: l, top: r, width: p, height: u } = s;
  u < L && (r = r - Math.round((L - u) / 2), u = L), p < L && (l = l - Math.round((L - p) / 2), p = L), e.style.position === "fixed" && (l +=
  window.scrollX, r += window.scrollY);
  let { x: m, y: f } = d;
  return m >= l && m <= l + p && f >= r && f <= r + u;
}, "isTargeted"), oe = /* @__PURE__ */ c((s, e, d = {}) => {
  let { x: l, y: r } = e, { margin: p = 5, topOffset: u = 0, centered: m = !1 } = d, { scrollX: f, scrollY: E, innerHeight: w, innerWidth: v } = window,
  I = Math.min(
    s.style.position === "fixed" ? r - E : r,
    w - s.clientHeight - p - u + E
  ), H = m ? s.clientWidth / 2 : 0, A = s.style.position === "fixed" ? Math.max(Math.min(l - f, v - H - p), H + p) : Math.max(
    Math.min(l, v - H - p + f),
    H + p + f
  );
  Object.assign(s.style, {
    ...A !== l && { left: `${A}px` },
    ...I !== r && { top: `${I}px` }
  });
}, "keepInViewport"), V = /* @__PURE__ */ c((s) => {
  window.HTMLElement.prototype.hasOwnProperty("showPopover") && s.showPopover();
}, "showPopover"), ne = /* @__PURE__ */ c((s) => {
  window.HTMLElement.prototype.hasOwnProperty("showPopover") && s.hidePopover();
}, "hidePopover"), se = /* @__PURE__ */ c((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", ie = "storybook-highlights-root", ge = "storybook-root", re = /* @__PURE__ */ c((s) => {
  if (globalThis.__STORYBOOK_HIGHLIGHT_INITIALIZED)
    return;
  globalThis.__STORYBOOK_HIGHLIGHT_INITIALIZED = !0;
  let { document: e } = globalThis, d = T([]), l = T(/* @__PURE__ */ new Map()), r = T([]), p = T(), u = T(), m = T([]), f = T([]), E = T(),
  w = T(), v = e.getElementById(ie);
  d.subscribe(() => {
    v || (v = b("div", { id: ie }), e.body.appendChild(v));
  }), d.subscribe((n) => {
    let t = e.getElementById(ge);
    if (!t)
      return;
    l.set(z(n));
    let o = new MutationObserver(() => l.set(z(n)));
    return o.observe(t, { subtree: !0, childList: !0 }), () => {
      o.disconnect();
    };
  }), l.subscribe((n) => {
    let t = /* @__PURE__ */ c(() => requestAnimationFrame(() => r.set(te(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: h, overflowX: x, overflowY: y } = window.getComputedStyle(i);
      return ["auto", "scroll"].some((M) => [h, x, y].includes(M));
    });
    return a.forEach((i) => i.addEventListener("scroll", t)), () => {
      o.disconnect(), a.forEach((i) => i.removeEventListener("scroll", t));
    };
  }), l.subscribe((n) => {
    let t = Array.from(n.keys()).filter(({ style: a }) => a.position === "sticky"), o = /* @__PURE__ */ c(() => requestAnimationFrame(() => {
      r.set(
        (a) => a.map((i) => {
          if (t.includes(i.element)) {
            let { top: h, left: x } = i.element.getBoundingClientRect();
            return { ...i, top: h + window.scrollY, left: x + window.scrollX };
          }
          return i;
        })
      );
    }), "updateBoxes");
    return e.addEventListener("scroll", o), () => e.removeEventListener("scroll", o);
  }), l.subscribe((n) => {
    m.set((t) => t.filter(({ element: o }) => n.has(o)));
  }), m.subscribe((n) => {
    n.length ? (w.set((t) => n.some((o) => o.element === t?.element) ? t : void 0), E.set((t) => n.some((o) => o.element === t?.element) ? t :
    void 0)) : (w.set(void 0), E.set(void 0), p.set(void 0));
  });
  let I = new Map(/* @__PURE__ */ new Map());
  d.subscribe((n) => {
    n.forEach(({ keyframes: t }) => {
      if (t) {
        let o = I.get(t);
        o || (o = e.createElement("style"), o.setAttribute("data-highlight", "keyframes"), I.set(t, o), e.head.appendChild(o)), o.innerHTML =
        t;
      }
    }), I.forEach((t, o) => {
      n.some((a) => a.keyframes === o) || (t.remove(), I.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, [b("div")])
        ), 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__ */ c((a) => {
      requestAnimationFrame(() => {
        let i = e.getElementById(g), h = { x: a.pageX, y: a.pageY };
        if (i && !P(i, h)) {
          let x = t.filter((y) => {
            let M = H.get(y.element);
            return X(y, M, h);
          });
          p.set(x.length ? h : void 0), m.set(x);
        }
      });
    }, "onClick");
    return e.addEventListener("click", o), () => e.removeEventListener("click", o);
  });
  let A = /* @__PURE__ */ c(() => {
    let n = e.getElementById(g), t = u.get();
    !t || n && P(n, t) || f.set((o) => {
      let a = r.get().filter((y) => {
        let M = H.get(y.element);
        return X(y, M, t);
      }), i = o.filter((y) => a.includes(y)), h = a.filter((y) => !o.includes(y)), x = o.length - i.length;
      return h.length || x ? [...i, ...h] : o;
    });
  }, "updateHovered");
  u.subscribe(A), r.subscribe(A);
  let $ = /* @__PURE__ */ c(() => {
    let n = w.get(), t = n ? [n] : m.get(), o = t.length === 1 ? t[0] : E.get(), a = p.get() !== void 0;
    r.get().forEach((i) => {
      let h = H.get(i.element);
      if (h) {
        let x = o === i, y = a ? o ? x : t.includes(i) : f.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 : {},
          ...x ? i.focusStyles : {},
          position: getComputedStyle(i.element).position === "fixed" ? "fixed" : "absolute",
          zIndex: F - 10,
          top: `${i.top}px`,
          left: `${i.left}px`,
          width: `${i.width}px`,
          height: `${i.height}px`,
          margin: 0,
          padding: 0,
          cursor: i.menu && y ? "pointer" : "default",
          pointerEvents: i.menu ? "auto" : "none",
          display: "flex",
          alignItems: "center",
          justifyContent: "center",
          overflow: "visible"
        }), Object.assign(h.children[0].style, {
          width: "100%",
          height: "100%",
          minHeight: `${L}px`,
          minWidth: `${L}px`,
          boxSizing: "content-box",
          padding: h.style.outlineWidth || "0px"
        }), V(h);
      }
    });
  }, "updateBoxStyles");
  r.subscribe($), m.subscribe($), f.subscribe($), E.subscribe($), w.subscribe($);
  let Y = /* @__PURE__ */ c(() => {
    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: ${F};
              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 = w.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 h = o.length > 1 && !!i.menu?.some(
            (M) => M.some(
              (O) => !O.selectors || O.selectors.some((B) => i.selectors.includes(B))
            )
          ), x = h ? {
            class: "selectable",
            onClick: /* @__PURE__ */ c(() => w.set(i), "onClick"),
            onMouseEnter: /* @__PURE__ */ c(() => E.set(i), "onMouseEnter"),
            onMouseLeave: /* @__PURE__ */ c(() => E.set(void 0), "onMouseLeave")
          } : t ? { class: "selected", onClick: /* @__PURE__ */ c(() => w.set(void 0), "onClick") } : {}, y = h || t;
          return b("li", x, [
            b(y ? "button" : "div", y ? { type: "button" } : {}, [
              t ? k("chevronLeft") : null,
              b("code", {}, [i.element.outerHTML]),
              h ? k("chevronRight") : null
            ])
          ]);
        })
      )
    )), w.get() || m.get().length === 1) {
      let i = w.get() || m.get()[0], h = i.menu?.filter(
        (x) => x.some(
          (y) => !y.selectors || y.selectors.some((M) => i.selectors.includes(M))
        )
      );
      h?.length && n.appendChild(
        b(
          "ul",
          { class: "menu-list" },
          h.map(
            (x) => b("li", {}, [
              b(
                "ul",
                { class: "menu-items" },
                x.map(
                  ({ id: y, title: M, description: O, iconLeft: B, iconRight: q, clickEvent: W }) => {
                    let N = W && (() => s.emit(W, y, se(i)));
                    return b("li", {}, [
                      b(
                        N ? "button" : "div",
                        N ? { class: "menu-item", type: "button", onClick: N } : { class: "menu-item" },
                        [
                          B ? k(B) : null,
                          b("div", { class: "menu-item-content" }, [
                            b(O ? "strong" : "span", {}, [M]),
                            O && b("span", {}, [O])
                          ]),
                          q ? k(q) : 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`
    }), V(n), requestAnimationFrame(() => oe(n, a, { topOffset: 15, centered: !0 }))) : (ne(n), Object.assign(n.style, { display: "none" }));
  }, "renderMenu");
  m.subscribe(Y), w.subscribe(Y);
  let ce = /* @__PURE__ */ c((n) => {
    let t = ee(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__ */ c((n) => {
    n && d.set((t) => t.filter((o) => o.id !== n));
  }, "removeHighlight"), Z = /* @__PURE__ */ c(() => {
    d.set([]), l.set(/* @__PURE__ */ new Map()), r.set([]), p.set(void 0), u.set(void 0), m.set([]), f.set([]), E.set(void 0), w.set(void 0);
  }, "resetState"), j, de = /* @__PURE__ */ c((n, t) => {
    let o = "scrollIntoView-highlight";
    clearTimeout(j), 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((h) => [
      ...h,
      {
        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; }
        }`
      }
    ]), j = setTimeout(() => D(o), 3500);
  }, "scrollIntoView"), ae = /* @__PURE__ */ c((n) => {
    requestAnimationFrame(() => u.set({ x: n.pageX, y: n.pageY }));
  }, "onMouseMove");
  e.body.addEventListener("mousemove", ae), s.on(U, ce), s.on(K, D), s.on(J, Z), s.on(Q, de), s.on(he, ({ newPhase: n }) => {
    n === "loading" && Z();
  });
}, "useHighlights");

// src/highlight/preview.ts
globalThis?.FEATURES?.highlight && le?.ready && le.ready().then(re);
var Se = /* @__PURE__ */ c(() => fe({}), "default");
export {
  Se as default
};
