import { f as D, a as M } from "./packages/sdk/dist/shared/chunk-NLEWXPVM.js";
import "@sinclair/typebox";
import "./packages/sdk/dist/shared/chunk-AQPN5SE2.js";
import "./packages/sdk/dist/shared/chunk-DQXDGV67.js";
import "./packages/sdk/dist/shared/chunk-7DMUDZ4H.js";
import "./packages/sdk/dist/shared/chunk-LSPN22SI.js";
import "./packages/sdk/dist/shared/chunk-ETYYRWLK.js";
import "./packages/sdk/dist/shared/chunk-4TH3C76A.js";
import "./packages/sdk/dist/shared/chunk-FKNNTHWX.js";
import "./packages/sdk/dist/shared/chunk-JS4K5JRM.js";
import "./packages/sdk/dist/shared/chunk-WU6Q4WYD.js";
import "./packages/sdk/dist/shared/chunk-QISTDGPP.js";
import "./packages/sdk/dist/shared/chunk-LFQZRGHS.js";
import "./packages/sdk/dist/shared/chunk-SLRU4CT6.js";
import "./packages/sdk/dist/shared/chunk-TTTXV7TJ.js";
import "./packages/sdk/dist/shared/chunk-UHF6IEKV.js";
import "./packages/sdk/dist/shared/chunk-LGP6GWIY.js";
import "./packages/sdk/dist/shared/chunk-X43MB5WD.js";
import "./packages/sdk/dist/shared/chunk-NNI225RG.js";
import "./packages/sdk/dist/shared/chunk-ZQGNYK5I.js";
import "./packages/sdk/dist/shared/chunk-KHFNN4P3.js";
import "./packages/sdk/dist/shared/chunk-BZGNY7LB.js";
import "./packages/sdk/dist/shared/chunk-IIFTGWFZ.js";
import { a as v } from "./packages/sdk/dist/shared/chunk-MPBYGS6W.js";
import "./packages/sdk/dist/shared/chunk-UU2MSQXY.js";
import "./packages/sdk/dist/shared/chunk-T3LIA4YB.js";
import "./packages/sdk/dist/shared/chunk-YBGPPF7T.js";
import "./packages/sdk/dist/shared/chunk-UMHXXXVP.js";
import "./packages/sdk/dist/shared/chunk-5N6MBJJ4.js";
import P from "./packages/sdk/dist/shared/utils/invariant.js";
import { enableMapSet as y } from "./node_modules/.pnpm/immer@10.1.1/node_modules/immer/dist/immer.js";
import { debounce as E, isEqual as C, merge as q } from "lodash-es";
import { createContext as F, useContext as L, useEffect as w } from "react";
import { temporal as Q } from "./node_modules/.pnpm/zundo@2.3.0_zustand@4.5.5_@types_react@18.3.3_immer@10.1.1_react@18.3.0-canary-3d9b20132-20240124_/node_modules/zundo/dist/index.js";
import { useStore as b } from "./node_modules/.pnpm/zustand@4.5.5_@types_react@18.3.3_immer@10.1.1_react@18.3.0-canary-3d9b20132-20240124/node_modules/zustand/esm/index.js";
import { subscribeWithSelector as O } from "./node_modules/.pnpm/zustand@4.5.5_@types_react@18.3.3_immer@10.1.1_react@18.3.0-canary-3d9b20132-20240124/node_modules/zustand/esm/middleware.js";
import { immer as U } from "./node_modules/.pnpm/zustand@4.5.5_@types_react@18.3.3_immer@10.1.1_react@18.3.0-canary-3d9b20132-20240124/node_modules/zustand/esm/middleware/immer.js";
import { createStore as W } from "./node_modules/.pnpm/zustand@4.5.5_@types_react@18.3.3_immer@10.1.1_react@18.3.0-canary-3d9b20132-20240124/node_modules/zustand/esm/vanilla.js";
y();
const Ce = (c) => {
  const i = {
    data: {},
    mode: "local",
    brickMap: x(c.sections),
    themes: [],
    pages: [],
    datasources: [],
    datarecords: [],
    queries: [],
    tags: [],
    sitePrompt: ""
  };
  return W()(
    O(
      Q(
        U((n, d) => ({
          ...i,
          ...c,
          upsertQuery: (e) => n((r) => {
            const t = r.siteAttributes.queries?.findIndex((o) => o.id === e.id) ?? -1;
            t !== -1 ? r.siteAttributes.queries[t] = e : (r.siteAttributes.queries ??= [], r.siteAttributes.queries.push(e));
          }),
          detachBrickFromContainer: (e) => n((r) => {
            const t = r.brickMap.get(e);
            P(t, `Cannot detach brick ${e}, it does not exist in the brickMap`);
            const { brick: o, sectionId: s, parentId: a } = t;
            if (!a) {
              console.warn("Cannot detach brick %s, it is not in a container", e);
              return;
            }
            const l = g(a, r);
            if (!l) {
              console.error("Cannot detach brick %s, its parent %s does not exist", e, a);
              return;
            }
            const h = r.sections.find((u) => u.id === s);
            if (!h) {
              console.error("Cannot detach brick %s, its parent section %s does not exist", e, s);
              return;
            }
            l.props.$children = l.props.$children?.filter(
              (u) => u.id !== e
            );
            const p = h.bricks.findIndex((u) => u.id === a);
            h.bricks.splice(p + 1, 0, {
              ...o
            }), r.brickMap = x(r.sections);
          }),
          createEmptySection: (e, r) => n((t) => {
            const o = t.sections.length, s = 1 + (r ? t.sections.find((l) => l.id === r)?.order ?? o : o), a = {
              id: e,
              order: s,
              label: `Section ${o + 1}`,
              bricks: [],
              props: {}
            };
            t.sections.forEach((l) => {
              l.order >= s && (l.order += 1);
            }), t.sections.push(a);
          }),
          addPage: (e) => n((r) => {
            r.pages.push(e), r.id === "_default_" && (r.id = e.id, r.path = e.path, r.label = e.label, r.sections = e.sections, e.attributes && (r.pageAttributes = e.attributes));
          }),
          isFirstSection: (e) => d().sections.find((t) => t.order === 0)?.id === e,
          setSections: (e) => n((r) => {
            r.sections = e, r.brickMap = x(e);
          }),
          pickTheme: (e) => n((r) => {
            const t = r.themes.find((o) => o.id === e);
            if (!t) {
              console.error("Cannot pick theme %s, it does not exist", e);
              return;
            }
            r.theme = t;
          }),
          setThemes: (e) => n((r) => {
            r.themes = e;
          }),
          setSitemap: (e) => n((r) => {
            r.sitemap = e;
          }),
          getPageDataForDuplication: () => {
            const e = d(), r = e.sitemap.length + 1;
            return {
              id: `page-${M()}`,
              label: `${e.label} (page ${r})`,
              path: `${e.path}-${r}`,
              sections: e.sections,
              pageAttributes: e.pageAttributes,
              siteAttributes: e.siteAttributes,
              datasources: e.datasources,
              datarecords: e.datarecords
            };
          },
          isLastSection: (e) => {
            const r = d();
            return r.sections.find((t) => t.order === r.sections.length - 1)?.id === e;
          },
          addDatasource: (e) => n((r) => {
            if (r.datasources.find((o) => o.id === e.id)) {
              console.error("Cannot add datasource %s, it already exists", e.id);
              return;
            }
            r.datasources.push(e);
          }),
          addDatarecord: (e) => n((r) => {
            if (r.datarecords.find((o) => o.id === e.id)) {
              console.error("Cannot add datarecord %s, it already exists", e.id);
              return;
            }
            r.datarecords.push(e);
          }),
          addSection: (e) => n((r) => {
            r.sections.push(e);
          }),
          updateSection: (e, r) => n((t) => {
            const o = t.sections.findIndex((s) => s.id === e);
            o !== -1 && (t.sections[o] = {
              ...t.sections[o],
              ...r
            });
          }),
          duplicateSection: (e) => n((r) => {
            const t = r.sections.find((a) => a.id === e);
            if (!t) {
              console.error("Cannot duplicate section %s, it does not exist", e);
              return;
            }
            const o = r.sections.findIndex((a) => a.id === e), s = {
              ...t,
              id: `s_${M()}`,
              order: t.order + 1,
              // increment order to place it after the original
              label: `${t.label} (copy)`,
              // generate new bricks with new IDs
              bricks: t.bricks.map((a) => ({
                ...a,
                id: `b_${M()}`,
                props: v({}, a.props, {
                  $children: a.props.$children?.map((l) => ({
                    ...l,
                    id: `b_${M()}`
                  }))
                })
              }))
            };
            r.sections.splice(o + 1, 0, s), r.brickMap = x(r.sections);
          }),
          deleteSection: (e) => n((r) => {
            if (!r.sections.find((o) => o.id === e))
              return console.warn("Cannot delete section %s: not found", e), null;
            r.brickMap.forEach((o) => {
              o.sectionId === e && r.brickMap.delete(o.brick.id);
            }), r.sections = r.sections.filter((o) => o.id !== e);
          }),
          getSection: (e) => d().sections.find((r) => r.id === e),
          moveSectionUp: (e) => n((r) => {
            const t = r.sections.find((a) => a.id === e);
            r.sections.findIndex((a) => a.id === e), P(t, "Section not found");
            const o = r.sections.find((a) => a.order === t.order - 1);
            r.sections.findIndex((a) => a.order === t.order - 1), P(o, "Previous section not found");
            const s = t.order;
            t.order = o.order, o.order = s;
          }),
          moveSectionDown: (e) => n((r) => {
            const t = r.sections.find((p) => p.id === e), o = r.sections.findIndex((p) => p.id === e);
            P(t, "Section not found");
            const s = r.sections.filter((p) => p.order > t.order).toSorted((p, u) => p.order - u.order).at(0);
            P(s, `Next section not found. No section with order > ${t.order} exists.`);
            const a = r.sections.findIndex((p) => p.id === s?.id), l = t.order, h = s.order;
            r.sections[o].order = h, r.sections[a].order = l;
          }),
          reorderSections: (e) => n((r) => {
            const t = [];
            let o = 0;
            e.forEach((s) => {
              const a = r.sections.find((l) => l.id === s);
              a && t.push({ ...a, order: ++o });
            }), r.sections.forEach((s) => {
              e.includes(s.id) || t.push({ ...s, order: ++o });
            }), r.sections = t, r.brickMap = x(r.sections);
          }),
          deleteBrick: (e) => n((r) => {
            const t = r.brickMap.get(e);
            if (!t) {
              console.error("Cannot delete brick %s, it does not exist", e);
              return;
            }
            const o = [], s = (p) => {
              const u = r.brickMap.get(p);
              u?.brick.props?.$children && u.brick.props.$children.forEach((k) => {
                o.push(k.id), s(k.id);
              });
            };
            s(e);
            const { sectionId: a, parentId: l } = t, h = r.sections.find((p) => p.id === a);
            if (l) {
              const p = g(l, r);
              if (p?.props.$children) {
                const u = p.props.$children.findIndex((f) => f.id === e);
                p.props.$children.splice(u, 1);
              }
            } else h && (h.bricks = h.bricks.filter((p) => p.id !== e));
            r.brickMap.delete(e), o.forEach((p) => {
              r.brickMap.delete(p);
            });
          }),
          duplicateBrick: (e) => n((r) => {
            const t = r.brickMap.get(e);
            if (!t) {
              console.error("Cannot duplicate brick %s, it does not exist", e);
              return;
            }
            const o = (p) => {
              const u = `brick-${M()}`, f = {
                ...p,
                id: u,
                props: { ...p.props },
                mobileProps: p.mobileProps ? { ...p.mobileProps } : void 0
              };
              if (p.props.$children) {
                const k = p.props.$children;
                f.props.$children = k.map((S) => o(S));
              }
              return f;
            }, s = o(t.brick), a = (p, u, f) => {
              r.brickMap.set(p.id, {
                brick: p,
                sectionId: u,
                parentId: f
              }), p.props.$children && p.props.$children.forEach((S) => a(S, u, p.id));
            }, { sectionId: l, parentId: h } = t;
            if (h) {
              const p = g(h, r);
              if (p?.props.$children) {
                const f = p.props.$children.findIndex((k) => k.id === e);
                p.props.$children.splice(f + 1, 0, s);
              }
            } else {
              const p = r.sections.find((u) => u.id === l);
              if (p) {
                const u = p.bricks.findIndex((f) => f.id === e);
                p.bricks.splice(u + 1, 0, s);
              }
            }
            a(s, l, h);
          }),
          updatePropsMapping: (e, r) => n((t) => {
            const o = g(e, t);
            o ? (o.propsMapping = q({}, o.propsMapping, r), o.props.lastTouched = Date.now(), t.brickMap = x(t.sections)) : console.error(
              "Cannot update props mapping for brick %s, it does not exist in the brick map",
              e
            );
          }),
          updateBrickProps: (e, r, t) => n((o) => {
            const s = g(e, o);
            s ? (t ? s.mobileProps = v({}, s.mobileProps, r, {
              lastTouched: Date.now()
            }) : s.props = v({}, s.props, r, {
              lastTouched: Date.now()
            }), o.brickMap = x(o.sections)) : console.error("Cannot update props for brick %s, it does not exist in the brick map", e);
          }),
          updateSectionProps: (e, r, t) => n((o) => {
            const s = o.sections.find((a) => a.id === e);
            s ? t ? s.mobileProps = v({}, s.mobileProps, r, {
              lastTouched: Date.now()
            }) : s.props = v({}, s.props, r, {
              lastTouched: Date.now()
            }) : console.warn("Cannot update section %s, it does not exist", e);
          }),
          /**
           * Move a brick inside its container or its section
           * If the brick does not belong to a container, it will be moved within its section
           */
          moveBrick: (e, r) => n((t) => {
            let o = [], s = -1;
            const a = t.getParentBrick(e);
            a ? (o = g(a.id, t).props.$children, s = o.findIndex((u) => u.id === e)) : (o = T(e, t).bricks, s = o.findIndex((u) => u.id === e));
            const l = r === "previous" ? Math.max(0, s - 1) : Math.min(o.length - 1, s + 1);
            if (l === s)
              return;
            const [h] = o.splice(s, 1);
            o.splice(l, 0, h), t.brickMap = x(t.sections);
          }),
          /**
           * Reorder a brick within its section using indices
           */
          reorderBrickWithin: (e, r) => n((t) => {
            const o = t.brickMap.get(e);
            if (!o) {
              console.error("Cannot reorder brick %s, brick mapping not found", e);
              return;
            }
            const { sectionId: s, parentId: a } = o;
            if (console.debug("Reordering brick %s in section %s, parent %s", e, s, a), a) {
              const l = g(a, t);
              if (l?.props.$children) {
                const h = l.props.$children, p = h.findIndex((f) => f.id === e);
                if (p === -1) {
                  console.error("Cannot reorder brick %s, it does not exist in parent's children", e), console.log({ sectionId: s, parentId: a });
                  return;
                }
                const [u] = h.splice(p, 1);
                h.splice(r, 0, u);
              } else
                console.error(
                  "Cannot reorder brick %s, parent brick %s has no children",
                  e,
                  a
                );
            } else {
              const l = t.sections.find((h) => h.id === s);
              if (l) {
                const h = l.bricks.findIndex((u) => u.id === e);
                if (h === -1) {
                  console.error(
                    "Cannot reorder brick %s, it does not exist in section %s",
                    e,
                    s
                  );
                  return;
                }
                const [p] = l.bricks.splice(h, 1);
                l.bricks.splice(r, 0, p);
              }
            }
          }),
          moveBrickToContainerBrick: (e, r, t) => n((o) => {
            const s = o.getBrick(e), a = g(r, o), l = o.brickMap.get(e);
            if (!s || !a || !l) {
              console.error("Cannot move brick %s to new parent, brick or new parent is null", e);
              return;
            }
            if (!("$children" in a.props)) {
              console.error("Cannot move brick %s, parent brick %s is not a container", e, r);
              return;
            }
            const { sectionId: h, parentId: p } = l;
            if (p) {
              const f = g(p, o);
              f?.props.$children && (f.props.$children = f.props.$children.filter(
                (k) => k.id !== e
              ));
            } else {
              const f = o.sections.find((k) => k.id === h);
              f && (f.bricks = f.bricks.filter((k) => k.id !== e));
            }
            a.props.$children || (a.props.$children = []), a.props.$children.splice(t, 0, s);
            const u = o.brickMap.get(r);
            if (u) {
              o.brickMap.set(e, {
                brick: s,
                sectionId: u.sectionId,
                parentId: r
              });
              const f = (k, S) => {
                const $ = o.brickMap.get(k);
                $?.brick.props?.$children && $.brick.props.$children.forEach((B) => {
                  o.brickMap.set(B.id, {
                    brick: B,
                    sectionId: S,
                    parentId: k
                  }), f(B.id, S);
                });
              };
              f(e, u.sectionId);
            }
          }),
          moveBrickToSection: (e, r, t) => n((o) => {
            const s = o.getBrick(e);
            r || (r = o.brickMap.get(e)?.sectionId);
            const a = o.sections.find((f) => f.id === r), l = o.brickMap.get(e);
            if (!s || !a || !l) {
              console.error(
                "Cannot move brick %s to section %s, brick or section not found",
                e,
                r
              );
              return;
            }
            const { sectionId: h, parentId: p } = l;
            if (p) {
              const f = g(p, o);
              f?.props.$children && (f.props.$children = f.props.$children.filter(
                (k) => k.id !== e
              ));
            } else {
              const f = o.sections.find((k) => k.id === h);
              f && (f.bricks = f.bricks.filter((k) => k.id !== e));
            }
            typeof t == "number" ? a.bricks.splice(t, 0, s) : a.bricks.push(s), o.brickMap.set(e, {
              brick: s,
              sectionId: r,
              parentId: null
              // Top level in section
            });
            const u = (f, k, S) => {
              const $ = o.brickMap.get(f);
              $?.brick.props?.$children && $.brick.props.$children.forEach((B) => {
                o.brickMap.set(B.id, {
                  brick: B,
                  sectionId: k,
                  parentId: f
                }), u(B.id, k);
              });
            };
            u(e, r);
          }),
          /**
           * @deprecated Use getBrickFromDraft instead
           */
          getBrick: (e) => d().brickMap.get(e)?.brick,
          getParentBrick: (e) => {
            const r = d().brickMap, { parentId: t } = r.get(e) ?? {};
            if (t)
              return r.get(t)?.brick;
          },
          getPositionWithinParent: (e) => {
            const r = d().getParentBrick(e);
            if (r && "$children" in r.props)
              return r.props.$children.findIndex((t) => t.id === e);
          },
          canMoveTo: (e, r) => {
            const t = d().getParentBrick(e);
            if (t && "$children" in t.props) {
              const a = t.props.$children, l = a.findIndex((h) => h.id === e);
              return r === "previous" ? l > 0 : l < a.length - 1;
            }
            const o = T(e, d()), s = o?.bricks.findIndex((a) => a.id === e) ?? -1;
            return r === "previous" ? s > 0 : s < o?.bricks.length - 1;
          },
          setPreviewTheme: (e) => n((r) => {
            r.previewTheme = e;
          }),
          validatePreviewTheme: (e) => n((r) => {
            r.previewTheme && e && (r.theme = r.previewTheme), r.previewTheme = void 0;
          }),
          cancelPreviewTheme: () => n((e) => {
            e.previewTheme = void 0;
          }),
          setTheme: (e) => n((r) => {
            r.theme = e;
          }),
          toggleBrickVisibility: (e, r) => n((t) => {
            const o = g(e, t);
            o && (o.props.hidden ??= { desktop: !1, mobile: !1 }, o.props.hidden[r] = !o.props.hidden?.[r]);
          }),
          addBrick: (e, r, t, o) => n((s) => {
            const a = s.sections.find((l) => l.id === r);
            if (!a) {
              console.error("Cannot add brick, section %s does not exist", r);
              return;
            }
            if (o) {
              if (console.log("Adding brick to parent container", o), !z(o, s)) {
                console.warn("Cannot add brick, parent container %s does not exist", o);
                return;
              }
              const h = s.sections.find((u) => u.id === r);
              if (!h) {
                console.warn("Cannot add brick, parent section %s does not exist", r);
                return;
              }
              const p = h.bricks.find((u) => u.id === o);
              if (!p) {
                console.warn(
                  "Cannot add brick, parent brick %s does not exist in section %s",
                  o,
                  r
                );
                return;
              }
              p.props.$children || (console.warn("Brick added to a container without $children, initializing it"), p.props.$children = []), p.props.$children.push(e);
            } else
              a.bricks.splice(t, 0, e);
            if (s.brickMap.set(e.id, {
              brick: e,
              sectionId: r,
              parentId: o
            }), e.props?.$children) {
              const l = (h, p) => {
                h.forEach((u) => {
                  s.brickMap.set(u.id, {
                    brick: u,
                    sectionId: r,
                    parentId: p
                  }), u.props?.$children && l(u.props.$children, u.id);
                });
              };
              console.log("Adding children to brick map", e.props.$children), l(e.props.$children, e.id);
            }
          }),
          updatePageAttributes: (e) => n((r) => {
            r.pageAttributes = { ...d().pageAttributes, ...e };
          }),
          updateSiteAttributes: (e) => n((r) => {
            r.siteAttributes = { ...d().siteAttributes, ...e };
          }),
          setVersion: (e) => n((r) => {
            r.version = e;
          }),
          setLastSaved: (e) => n((r) => {
            r.lastSaved = e;
          }),
          setDirty: (e) => n((r) => {
            r.dirty = e;
          })
        })),
        {
          // limit undo history to 100
          limit: 100,
          equality: (n, d) => C(n, d),
          partialize: (n) => Object.fromEntries(
            Object.entries(n).filter(([d]) => ["sections", "attr"].includes(d))
          )
          // handleSet: (handleSet) =>
          //   debounce<typeof handleSet>((state) => {
          //     if (state) {
          //       handleSet(state);
          //     }
          //   }, 200),
        }
      )
    )
  );
}, R = F(null), m = () => {
  const c = L(R);
  return P(c, "useDraftStoreContext must be used within a DraftStoreContext"), c;
}, Ae = () => m().temporal.getState(), Ie = () => {
  const c = m();
  return b(c, (i) => i.sitemap);
}, De = () => {
  const c = m();
  return b(c, (i) => {
    const n = i.sitemap.length > 0, d = i.themes.length > 0, e = n && d && i.sitemap.length > 0, r = new URL(window.location.href).searchParams.get("action") === "generate";
    return {
      hasSitemap: n,
      hasThemesGenerated: d,
      sitemap: i.sitemap,
      pages: i.pages,
      isReady: !r || e || !1
    };
  });
}, ye = () => {
  const c = m(), i = b(c, (d) => d.pages);
  return {
    site: V(),
    pages: i
  };
}, Ee = () => {
  const c = m();
  return b(c, (i) => i.themes);
}, qe = () => {
  const c = m();
  return b(c);
}, Fe = () => {
  const c = m();
  return b(c, (i) => i.dirty);
};
function Le(c) {
  const i = m(), n = b(i, (e) => e.getParentBrick);
  let d = n(c);
  for (; d; ) {
    if (d.type === "dynamic")
      return !0;
    c = d.id, d = n(c);
  }
  return !1;
}
function Qe() {
  const c = m();
  return b(c, (i) => i.siteAttributes.queries ?? []);
}
function Oe(c) {
  const i = m();
  return b(i, (n) => n.siteAttributes.queries?.find((d) => d.id === c) ?? null);
}
function Ue(c) {
  const i = m();
  return b(i, (d) => d.getParentBrick)(c);
}
function We() {
  const c = m();
  return b(c, (i) => i.version);
}
function Re() {
  const c = m();
  return b(c, (i) => i.lastSaved);
}
const Ve = () => {
  const c = m(), i = b(c, (n) => n.sections);
  return D(i);
}, ze = (c) => {
  const i = m();
  return b(i, (n) => {
    const d = n.sections.find((e) => c && e.id === c);
    return d ? {
      section: d,
      bricks: Array.from(n.brickMap).filter(([, e]) => e.sectionId === c)
    } : null;
  });
}, Ge = (c) => {
  const i = m();
  return b(i, (n) => c ? g(c, n) : null);
}, je = (c) => {
  const i = m();
  return b(i, (n) => T(c, n));
}, He = () => {
  const c = m();
  return b(c, (i) => i.pageAttributes);
}, Ne = () => {
  const c = m();
  return b(c, (i) => i.siteAttributes);
}, Ze = (c) => {
  const i = m(), n = A();
  return b(i, (d) => {
    if (!c)
      return d.data;
    const e = {};
    for (const r of n) {
      const t = r.datasource.schema.examples;
      t && (e[r.alias] = t);
    }
    return e;
  });
}, Je = (c) => A().find((n) => n.alias === c) ?? null;
function Ke(c) {
  const i = m(), n = b(i, (e) => e.getParentBrick);
  let d = c;
  for (; d; ) {
    if (g(d, i.getState())?.props.loop)
      return !0;
    d = n(d)?.id;
  }
  return !1;
}
function Xe(c) {
  const i = m(), n = b(i, (e) => e.getParentBrick);
  let d = c;
  for (; d; ) {
    const e = g(d, i.getState());
    if (e?.props.loop?.over)
      return e?.props.loop.over;
    d = n(d)?.id;
  }
  return null;
}
const A = () => {
  const c = m();
  return b(
    c,
    (i) => i.pageAttributes.queries?.map((n) => {
      const d = i.siteAttributes.queries?.find((r) => r.id === n.queryId);
      if (!d)
        return console.warn(`Query with id ${n.queryId} not found in the store`), null;
      const e = i.datasources.find((r) => r.id === d.datasourceId);
      return e ? {
        ...n,
        queryInfo: d,
        datasource: e
      } : (console.warn(
        `Datasource with id ${d.datasourceId} not found for query ${n.queryId}`
      ), null);
    }).filter((n) => n !== null) ?? []
  );
}, Ye = () => {
  const c = m();
  return b(c, (i) => ({
    deleteBrick: i.deleteBrick,
    detachBrickFromContainer: i.detachBrickFromContainer,
    upsertQuery: i.upsertQuery,
    setSections: i.setSections,
    setThemes: i.setThemes,
    setTheme: i.setTheme,
    pickTheme: i.pickTheme,
    setSitemap: i.setSitemap,
    addPage: i.addPage,
    addDatasource: i.addDatasource,
    addDatarecord: i.addDatarecord,
    duplicateBrick: i.duplicateBrick,
    addBrick: i.addBrick,
    getBrick: i.getBrick,
    validatePreviewTheme: i.validatePreviewTheme,
    duplicateSection: i.duplicateSection,
    toggleBrickVisibilityPerBreakpoint: i.toggleBrickVisibility,
    getParentBrick: i.getParentBrick,
    updateBrickProps: i.updateBrickProps,
    updatePropsMapping: i.updatePropsMapping,
    moveBrick: i.moveBrick,
    reorderBrickWithin: i.reorderBrickWithin,
    getPositionWithinParent: i.getPositionWithinParent,
    canMoveTo: i.canMoveTo,
    moveBrickToContainerBrick: i.moveBrickToContainerBrick,
    moveBrickToSection: i.moveBrickToSection,
    deleteSection: i.deleteSection,
    moveSectionUp: i.moveSectionUp,
    moveSectionDown: i.moveSectionDown,
    reorderSections: i.reorderSections,
    updateSection: i.updateSection,
    updateSectionProps: i.updateSectionProps,
    addSection: i.addSection,
    createEmptySection: i.createEmptySection,
    isFirstSection: i.isFirstSection,
    isLastSection: i.isLastSection
  }));
}, _e = () => {
  const c = m();
  return b(c, (i) => ({
    id: i.id,
    path: i.path,
    label: i.label,
    siteLabel: i.siteLabel,
    siteId: i.siteId,
    hostname: i.hostname
  }));
}, V = () => {
  const c = m();
  return b(c, (n) => ({
    id: n.siteId,
    label: n.label,
    sitemap: n.sitemap,
    theme: n.theme,
    themes: n.themes,
    attributes: n.siteAttributes,
    hostname: n.hostname,
    datasources: n.datasources,
    datarecords: n.datarecords,
    sitePrompt: n.sitePrompt
  }));
}, er = () => {
  const c = m();
  return b(c, (i) => i.theme);
}, rr = (c) => {
  const i = m();
  w(() => i.subscribe((n) => n?.sections ?? [], E(c, 200), {
    fireImmediately: !1
  }), []);
}, ir = (c) => {
  const i = m();
  w(() => i.subscribe((n) => n.pageAttributes, c, {
    equalityFn: C
  }), []);
}, nr = (c) => {
  const i = m();
  w(() => i.subscribe((n) => n.siteAttributes, c, {
    equalityFn: C
  }), []);
}, or = (c) => {
  const i = m();
  w(() => i.subscribe((n) => n.theme, c), []);
}, tr = () => {
  const c = m();
  return b(c, (i) => {
    const n = i.pageAttributes.path, d = /:([a-zA-Z0-9_]+)/g;
    return Array.from(n.matchAll(d)).map((r) => r[0]);
  });
}, cr = (c) => {
  const i = m();
  w(() => i.subscribe((n) => n.path, c), []);
};
function z(c, i) {
  const { brick: n } = i.brickMap.get(c) ?? {};
  return n;
}
function T(c, i) {
  const n = i.brickMap.get(c);
  if (n)
    return i.sections.find((d) => d.id === n.sectionId);
}
function g(c, i) {
  for (const n of i.sections) {
    const d = n.bricks.find((t) => t.id === c);
    if (d)
      return d;
    const e = (t) => {
      for (const o of t) {
        if (o.id === c)
          return o;
        if (o.props?.$children) {
          const s = e(o.props.$children);
          if (s)
            return s;
        }
      }
    }, r = e(n.bricks);
    if (r)
      return r;
  }
  return null;
}
const sr = () => {
  const c = m();
  return b(c, (i) => i.sitePrompt);
};
function x(c) {
  const i = /* @__PURE__ */ new Map();
  function n(d, e, r) {
    i.set(d.id, { brick: d, sectionId: e, parentId: r }), d.props?.$children && d.props.$children.forEach((o) => n(o, e, d.id));
  }
  return c.forEach((d) => {
    d.bricks.forEach((e) => n(e, d.id, null));
  }), i;
}
export {
  R as DraftStoreContext,
  Ce as createDraftStore,
  Fe as isDraftDirty,
  Ge as useBrick,
  Ze as useData,
  qe as useDraft,
  Ye as useDraftHelpers,
  Ae as useDraftUndoManager,
  De as useGenerationState,
  Le as useHasDynamicParent,
  Ke as useIsInLoop,
  Re as useLastSaved,
  Xe as useLoopAlias,
  Je as useLoopedQuery,
  He as usePageAttributes,
  ir as usePageAttributesSubscribe,
  m as usePageContext,
  _e as usePageInfo,
  tr as usePagePathParams,
  cr as usePagePathSubscribe,
  A as usePageQueries,
  We as usePageVersion,
  Ue as useParentBrick,
  ze as useSection,
  je as useSectionByBrickId,
  Ve as useSections,
  rr as useSectionsSubscribe,
  V as useSite,
  ye as useSiteAndPages,
  Ne as useSiteAttributes,
  nr as useSiteAttributesSubscribe,
  sr as useSitePrompt,
  Qe as useSiteQueries,
  Oe as useSiteQuery,
  Ie as useSitemap,
  er as useTheme,
  or as useThemeSubscribe,
  Ee as useThemes
};
//# sourceMappingURL=use-page-data.js.map
