import { j as y, a as B } from "./packages/sdk/dist/shared/chunk-GOMXZC4B.js";
import "@sinclair/typebox";
import "./packages/sdk/dist/shared/chunk-PSCN3VVA.js";
import "./packages/sdk/dist/shared/chunk-R6ATC4TC.js";
import "./packages/sdk/dist/shared/chunk-CFF6Q7O7.js";
import "./packages/sdk/dist/shared/chunk-IKC3FK7L.js";
import "./packages/sdk/dist/shared/chunk-E6XEI7IK.js";
import "./packages/sdk/dist/shared/chunk-SQ5T4AT5.js";
import "./packages/sdk/dist/shared/chunk-G5CJU6RD.js";
import "./packages/sdk/dist/shared/chunk-PSTFJ5IJ.js";
import "./packages/sdk/dist/shared/chunk-NJGXC7D6.js";
import "./packages/sdk/dist/shared/chunk-SXHDQ64Z.js";
import "./packages/sdk/dist/shared/chunk-QX6OGRRT.js";
import "./packages/sdk/dist/shared/chunk-FLOMNC6J.js";
import "./packages/sdk/dist/shared/chunk-PLSV746R.js";
import "./packages/sdk/dist/shared/chunk-WBZW5LVH.js";
import "./packages/sdk/dist/shared/chunk-WYVJE6J5.js";
import "./packages/sdk/dist/shared/chunk-I3VL6SRH.js";
import "./packages/sdk/dist/shared/chunk-B3EATRFX.js";
import "./packages/sdk/dist/shared/chunk-IKHZD2JH.js";
import "./packages/sdk/dist/shared/chunk-PXI7AHWS.js";
import { a as P } from "./packages/sdk/dist/shared/chunk-OIY3EPHO.js";
import "./packages/sdk/dist/shared/chunk-SY6DZLTC.js";
import "./packages/sdk/dist/shared/chunk-7A2JNQX3.js";
import "./packages/sdk/dist/shared/chunk-3YVJ2ELD.js";
import "./packages/sdk/dist/shared/chunk-AFVJZPLS.js";
import "./packages/sdk/dist/shared/chunk-A3GF4PYW.js";
import "./packages/sdk/dist/shared/chunk-W33JMRRN.js";
import { a as S } from "./packages/sdk/dist/shared/chunk-RDZK73KV.js";
import { enableMapSet as I } from "./node_modules/.pnpm/immer@10.1.1/node_modules/immer/dist/immer.js";
import { debounce as E, isEqual as w, unset as C, merge as A } from "lodash-es";
import { createContext as q, useContext as F, useEffect as $ } from "react";
import { temporal as L } 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 m } 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 Q } 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 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/middleware/immer.js";
import { createStore 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/vanilla.js";
I();
const Ae = (c) => {
  const r = {
    data: {},
    additionalAssets: [],
    brickMap: k(c.page.sections)
  };
  return O()(
    Q(
      L(
        W((t, d) => ({
          ...r,
          ...c,
          addAdditionalAssets: (e) => t((i) => {
            i.additionalAssets.push(...e);
          }),
          upsertQuery: (e) => t((i) => {
            const o = i.site.attributes.queries?.findIndex((n) => n.id === e.id) ?? -1;
            o !== -1 ? i.site.attributes.queries[o] = e : (i.site.attributes.queries ??= [], i.site.attributes.queries.push(e));
          }),
          detachBrickFromContainer: (e) => t((i) => {
            const o = i.brickMap.get(e);
            S(o, `Cannot detach brick ${e}, it does not exist in the brickMap`);
            const { brick: n, sectionId: s, parentId: p } = o;
            if (!p) {
              console.warn("Cannot detach brick %s, it is not in a container", e);
              return;
            }
            const l = x(p, i);
            if (!l) {
              console.error("Cannot detach brick %s, its parent %s does not exist", e, p);
              return;
            }
            const f = i.page.sections.find((u) => u.id === s);
            if (!f) {
              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 a = f.bricks.findIndex((u) => u.id === p);
            f.bricks.splice(a + 1, 0, {
              ...n
            }), i.brickMap = k(i.page.sections);
          }),
          createEmptySection: (e, i) => t((o) => {
            const n = o.page.sections.length, s = 1 + (i ? o.page.sections.find((l) => l.id === i)?.order ?? n : n), p = {
              id: e,
              order: s,
              label: `Section ${n + 1}`,
              bricks: [],
              props: {
                direction: "flex-row"
              }
            };
            o.page.sections.forEach((l) => {
              l.order >= s && (l.order += 1);
            }), o.page.sections.push(p);
          }),
          addPage: (e) => t((i) => {
            i.page = e, i.site.sitemap.find((n) => n.id === e.id) || i.site.sitemap.push(e), i.brickMap = k(i.page.sections);
          }),
          isFirstSection: (e) => d().page.sections.find((o) => o.order === 0)?.id === e,
          setSections: (e) => t((i) => {
            i.page.sections = e, i.brickMap = k(e);
          }),
          pickTheme: (e) => t((i) => {
            const o = i.site.themes.find((n) => n.id === e);
            if (!o) {
              console.error("Cannot pick theme %s, it does not exist", e);
              return;
            }
            i.site.theme = o;
          }),
          setThemes: (e) => t((i) => {
            const o = i.site.themes.length > 0;
            i.site.themes = e, o || (i.site.theme = e[0]);
          }),
          setSitemap: (e) => t((i) => {
            i.site.sitemap = e;
          }),
          getPageDataForDuplication: () => {
            const e = d(), i = e.site.sitemap.length + 1;
            return {
              ...e.page,
              id: `page-${B()}`,
              label: `${e.page.label} (page ${i})`,
              attributes: {
                ...e.page.attributes,
                path: `${e.page.attributes.path}-${i}`
              }
            };
          },
          isLastSection: (e) => {
            const i = d();
            return i.page.sections.find((o) => o.order === i.page.sections.length - 1)?.id === e;
          },
          addDatasource: (e) => t((i) => {
            if (i.site.datasources.find((n) => n.id === e.id)) {
              console.error("Cannot add datasource %s, it already exists", e.id);
              return;
            }
            i.site.datasources.push(e);
          }),
          addDatarecord: (e) => t((i) => {
            if (i.site.datarecords.find((n) => n.id === e.id)) {
              console.error("Cannot add datarecord %s, it already exists", e.id);
              return;
            }
            i.site.datarecords.push(e);
          }),
          addSection: (e) => t((i) => {
            i.page.sections.push(e), i.brickMap = k(i.page.sections);
          }),
          updateSection: (e, i) => t((o) => {
            const n = o.page.sections.findIndex((s) => s.id === e);
            n !== -1 && (o.page.sections[n] = {
              ...o.page.sections[n],
              ...i
            }), o.brickMap = k(o.page.sections);
          }),
          duplicateSection: (e) => t((i) => {
            const o = i.page.sections.find((p) => p.id === e);
            if (!o) {
              console.error("Cannot duplicate section %s, it does not exist", e);
              return;
            }
            const n = i.page.sections.findIndex((p) => p.id === e), s = {
              ...o,
              id: `s_${B()}`,
              order: o.order + 1,
              // increment order to place it after the original
              label: `${o.label} (copy)`,
              // generate new bricks with new IDs
              bricks: o.bricks.map((p) => ({
                ...p,
                id: `b_${B()}`,
                props: P({}, p.props, {
                  $children: p.props.$children?.map((l) => ({
                    ...l,
                    id: `b_${B()}`
                  }))
                })
              }))
            };
            i.page.sections.splice(n + 1, 0, s), i.brickMap = k(i.page.sections);
          }),
          deleteSection: (e) => t((i) => {
            if (!i.page.sections.find((n) => n.id === e))
              return console.warn("Cannot delete section %s: not found", e), null;
            i.brickMap.forEach((n) => {
              n.sectionId === e && i.brickMap.delete(n.brick.id);
            }), i.page.sections = i.page.sections.filter((n) => n.id !== e);
          }),
          getSection: (e) => d().page.sections.find((i) => i.id === e),
          moveSectionUp: (e) => t((i) => {
            const o = i.page.sections.find((p) => p.id === e);
            i.page.sections.findIndex((p) => p.id === e), S(o, "Section not found");
            const n = i.page.sections.find((p) => p.order === o.order - 1);
            i.page.sections.findIndex((p) => p.order === o.order - 1), S(n, "Previous section not found");
            const s = o.order;
            o.order = n.order, n.order = s, i.brickMap = k(i.page.sections);
          }),
          moveSectionDown: (e) => t((i) => {
            const o = i.page.sections.find((a) => a.id === e), n = i.page.sections.findIndex((a) => a.id === e);
            S(o, "Section not found");
            const s = i.page.sections.filter((a) => a.order > o.order).toSorted((a, u) => a.order - u.order).at(0);
            S(s, `Next section not found. No section with order > ${o.order} exists.`);
            const p = i.page.sections.findIndex((a) => a.id === s?.id), l = o.order, f = s.order;
            i.page.sections[n].order = f, i.page.sections[p].order = l, i.brickMap = k(i.page.sections);
          }),
          reorderSections: (e) => t((i) => {
            const o = [];
            let n = 0;
            e.forEach((s) => {
              const p = i.page.sections.find((l) => l.id === s);
              p && o.push({ ...p, order: ++n });
            }), i.page.sections.forEach((s) => {
              e.includes(s.id) || o.push({ ...s, order: ++n });
            }), i.page.sections = o, i.brickMap = k(i.page.sections);
          }),
          deleteBrick: (e) => t((i) => {
            const o = i.brickMap.get(e);
            if (!o) {
              console.error("Cannot delete brick %s, it does not exist", e);
              return;
            }
            const n = [], s = (a) => {
              const u = i.brickMap.get(a);
              u?.brick.props?.$children && u.brick.props.$children.forEach((b) => {
                n.push(b.id), s(b.id);
              });
            };
            s(e);
            const { sectionId: p, parentId: l } = o, f = i.page.sections.find((a) => a.id === p);
            if (l) {
              const a = x(l, i);
              if (a?.props.$children) {
                const u = a.props.$children.findIndex((h) => h.id === e);
                a.props.$children.splice(u, 1);
              }
            } else f && (f.bricks = f.bricks.filter((a) => a.id !== e));
            i.brickMap = k(i.page.sections);
          }),
          duplicateBrick: (e) => t((i) => {
            const o = i.brickMap.get(e);
            if (!o) {
              console.error("Cannot duplicate brick %s, it does not exist", e);
              return;
            }
            const n = (f) => {
              const a = `brick-${B()}`, u = {
                ...f,
                id: a,
                props: { ...f.props },
                mobileProps: f.mobileProps ? { ...f.mobileProps } : void 0
              };
              if (f.props.$children) {
                const h = f.props.$children;
                u.props.$children = h.map((b) => n(b));
              }
              return u;
            }, s = n(o.brick), { sectionId: p, parentId: l } = o;
            if (l) {
              const f = x(l, i);
              if (f?.props.$children) {
                const u = f.props.$children.findIndex((h) => h.id === e);
                f.props.$children.splice(u + 1, 0, s);
              }
            } else {
              const f = i.page.sections.find((a) => a.id === p);
              if (f) {
                const a = f.bricks.findIndex((u) => u.id === e);
                f.bricks.splice(a + 1, 0, s);
              }
            }
            i.brickMap = k(i.page.sections);
          }),
          updateBrickProps: (e, i, o) => t((n) => {
            const s = x(e, n);
            s ? (o ? s.mobileProps = P({}, s.mobileProps, i, {
              lastTouched: Date.now()
            }) : s.props = P({}, s.props, i, {
              lastTouched: Date.now()
            }), n.brickMap = k(n.page.sections)) : console.error("Cannot update props for brick %s, it does not exist in the brick map", e);
          }),
          updateSectionProps: (e, i, o) => t((n) => {
            const s = n.page.sections.find((p) => p.id === e);
            s ? (o ? s.mobileProps = P({}, s.mobileProps, i, {
              lastTouched: Date.now()
            }) : s.props = P({}, s.props, i, {
              lastTouched: Date.now()
            }), n.brickMap = k(n.page.sections)) : 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, i) => t((o) => {
            let n = [], s = -1;
            const p = o.getParentBrick(e);
            p ? (n = x(p.id, o).props.$children, s = n.findIndex((u) => u.id === e)) : (n = M(e, o).bricks, s = n.findIndex((u) => u.id === e));
            const l = i === "previous" ? Math.max(0, s - 1) : Math.min(n.length - 1, s + 1);
            if (l === s)
              return;
            const [f] = n.splice(s, 1);
            n.splice(l, 0, f), o.brickMap = k(o.page.sections);
          }),
          /**
           * Reorder a brick within its section using indices
           */
          reorderBrickWithin: (e, i) => t((o) => {
            const n = o.brickMap.get(e);
            if (!n) {
              console.error("Cannot reorder brick %s, brick mapping not found", e);
              return;
            }
            const { sectionId: s, parentId: p } = n;
            if (console.debug("Reordering brick %s in section %s, parent %s", e, s, p), p) {
              const l = x(p, o);
              if (l?.props.$children) {
                const f = l.props.$children, a = f.findIndex((h) => h.id === e);
                if (a === -1) {
                  console.error("Cannot reorder brick %s, it does not exist in parent's children", e), console.log({ sectionId: s, parentId: p });
                  return;
                }
                const [u] = f.splice(a, 1);
                f.splice(i, 0, u);
              } else
                console.error(
                  "Cannot reorder brick %s, parent brick %s has no children",
                  e,
                  p
                );
            } else {
              const l = o.page.sections.find((f) => f.id === s);
              if (l) {
                const f = l.bricks.findIndex((u) => u.id === e);
                if (f === -1) {
                  console.error(
                    "Cannot reorder brick %s, it does not exist in section %s",
                    e,
                    s
                  );
                  return;
                }
                const [a] = l.bricks.splice(f, 1);
                l.bricks.splice(i, 0, a);
              }
            }
            o.brickMap = k(o.page.sections);
          }),
          moveBrickToContainerBrick: (e, i, o) => t((n) => {
            const s = n.getBrick(e), p = x(i, n), l = n.brickMap.get(e);
            if (!s || !p || !l) {
              console.error("Cannot move brick %s to new parent, brick or new parent is null", e);
              return;
            }
            if (!("$children" in p.props)) {
              console.error("Cannot move brick %s, parent brick %s is not a container", e, i);
              return;
            }
            const { sectionId: f, parentId: a } = l;
            if (a) {
              const u = x(a, n);
              u?.props.$children && (u.props.$children = u.props.$children.filter(
                (h) => h.id !== e
              ));
            } else {
              const u = n.page.sections.find((h) => h.id === f);
              u && (u.bricks = u.bricks.filter((h) => h.id !== e));
            }
            p.props.$children || (p.props.$children = []), p.props.$children.splice(o, 0, s), n.brickMap = k(n.page.sections);
          }),
          /**
           * @todo check bricksmap update
           */
          moveBrickToSection: (e, i, o) => t((n) => {
            const s = n.getBrick(e);
            i || (i = n.brickMap.get(e)?.sectionId);
            const p = n.page.sections.find((h) => h.id === i), l = n.brickMap.get(e);
            if (!s || !p || !l) {
              console.error(
                "Cannot move brick %s to section %s, brick or section not found",
                e,
                i
              );
              return;
            }
            const { sectionId: f, parentId: a } = l;
            if (a) {
              const h = x(a, n);
              h?.props.$children && (h.props.$children = h.props.$children.filter(
                (b) => b.id !== e
              ));
            } else {
              const h = n.page.sections.find((b) => b.id === f);
              h && (h.bricks = h.bricks.filter((b) => b.id !== e));
            }
            typeof o == "number" ? p.bricks.splice(o, 0, s) : p.bricks.push(s), n.brickMap.set(e, {
              brick: s,
              sectionId: i,
              parentId: null
              // Top level in section
            });
            const u = (h, b, z) => {
              const T = n.brickMap.get(h);
              T?.brick.props?.$children && T.brick.props.$children.forEach((v) => {
                n.brickMap.set(v.id, {
                  brick: v,
                  sectionId: b,
                  parentId: h
                }), u(v.id, b);
              });
            };
            u(e, i);
          }),
          /**
           * @deprecated Use getBrickFromDraft instead
           */
          getBrick: (e) => d().brickMap.get(e)?.brick,
          getParentBrick: (e) => {
            const i = d().brickMap, { parentId: o } = i.get(e) ?? {};
            if (o)
              return i.get(o)?.brick;
          },
          getPositionWithinParent: (e) => {
            const i = d().getParentBrick(e);
            if (i && "$children" in i.props)
              return i.props.$children.findIndex((o) => o.id === e);
          },
          canMoveTo: (e, i) => {
            const o = d().getParentBrick(e);
            if (o && "$children" in o.props) {
              const p = o.props.$children, l = p.findIndex((f) => f.id === e);
              return i === "previous" ? l > 0 : l < p.length - 1;
            }
            const n = M(e, d()), s = n?.bricks.findIndex((p) => p.id === e) ?? -1;
            return i === "previous" ? s > 0 : s < n?.bricks.length - 1;
          },
          setPreviewTheme: (e) => t((i) => {
            i.previewTheme = e;
          }),
          validatePreviewTheme: (e) => t((i) => {
            i.previewTheme && e && (i.site.theme = i.previewTheme), i.previewTheme = void 0;
          }),
          cancelPreviewTheme: () => t((e) => {
            e.previewTheme = void 0;
          }),
          setTheme: (e) => t((i) => {
            i.site.theme = e;
          }),
          toggleBrickVisibility: (e, i) => t((o) => {
            const n = x(e, o);
            n && (n.props.hidden ??= { desktop: !1, mobile: !1 }, n.props.hidden[i] = !n.props.hidden?.[i]);
          }),
          addBrick: (e, i, o, n) => t((s) => {
            const p = s.page.sections.find((l) => l.id === i);
            if (!p) {
              console.error("Cannot add brick, section %s does not exist", i);
              return;
            }
            if (n) {
              if (console.log("Adding brick to parent container", n), !j(n, s)) {
                console.warn("Cannot add brick, parent container %s does not exist", n);
                return;
              }
              const f = s.page.sections.find((u) => u.id === i);
              if (!f) {
                console.warn("Cannot add brick, parent section %s does not exist", i);
                return;
              }
              const a = f.bricks.find((u) => u.id === n);
              if (!a) {
                console.warn(
                  "Cannot add brick, parent brick %s does not exist in section %s",
                  n,
                  i
                );
                return;
              }
              a.props.$children || (console.warn("Brick added to a container without $children, initializing it"), a.props.$children = []), a.props.$children.push(e);
            } else
              p.bricks.splice(o, 0, e);
            if (s.brickMap.set(e.id, {
              brick: e,
              sectionId: i,
              parentId: n ?? null
            }), e.props?.$children) {
              const l = (f, a) => {
                f.forEach((u) => {
                  s.brickMap.set(u.id, {
                    brick: u,
                    sectionId: i,
                    parentId: a
                  }), 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) => t((i) => {
            i.page.attributes = A({}, d().page.attributes, e);
          }),
          deletePageAttribute: (e) => t((i) => {
            C(i.page.attributes, e);
          }),
          updateSiteAttributes: (e) => t((i) => {
            i.site.attributes = A({}, d().site.attributes, e);
          }),
          deleteSiteAttribute: (e) => t((i) => {
            C(i.site.attributes, e);
          }),
          setVersion: (e) => t((i) => {
            i.page.version = e;
          }),
          setLastSaved: (e) => t((i) => {
            i.lastSaved = e;
          }),
          setDirty: (e) => t((i) => {
            i.dirty = e;
          })
        })),
        {
          // limit undo history to 100
          limit: 100,
          equality: (t, d) => w(t, d),
          partialize: (t) => Object.fromEntries(
            Object.entries(t).filter(([d]) => ["sections", "attr"].includes(d))
          )
          // handleSet: (handleSet) =>
          //   debounce<typeof handleSet>((state) => {
          //     if (state) {
          //       handleSet(state);
          //     }
          //   }, 200),
        }
      )
    )
  );
}, R = q(null), g = () => {
  const c = F(R);
  return S(c, "useDraftStoreContext must be used within a DraftStoreContext"), c;
}, De = () => g().temporal.getState(), ye = () => {
  const c = g();
  return m(c, (r) => r.site.sitemap);
}, Ie = () => {
  const c = g();
  return m(c, (r) => {
    const t = r.site.sitemap.length > 0, d = r.site.themes.length > 0, e = r.page.id !== "_default_", i = t && d && e && r.page.sections.length > 0 && r.page.sections.some((n) => n.bricks.length > 0), o = new URL(window.location.href).searchParams.get("action") === "setup";
    return {
      hasSitemap: t,
      hasThemesGenerated: d,
      isSetup: o,
      isReady: !o || i
    };
  });
}, Ee = () => {
  const c = g();
  return m(c, (r) => r.site.themes);
}, qe = () => {
  const c = g();
  return m(c);
};
function Fe() {
  const c = g();
  return m(c, (r) => r.site.attributes.queries ?? []);
}
function Le(c) {
  const r = g();
  return m(r, (t) => t.site.attributes.queries?.find((d) => d.id === c) ?? null);
}
function Qe(c) {
  const r = g();
  return m(r, (d) => d.getParentBrick)(c);
}
function We() {
  const c = g();
  return m(c, (r) => r.page.version);
}
function Oe() {
  const c = g();
  return m(c, (r) => r.lastSaved);
}
const Re = () => {
  const c = g(), r = m(c, (e) => e.page.sections), t = V(), d = U();
  return y(r, t, d);
}, Ue = (c) => {
  const r = g();
  return m(r, (t) => {
    const d = t.page.sections.find((e) => c && e.id === c);
    return d ? {
      section: d,
      bricks: Array.from(t.brickMap).filter(([, e]) => e.sectionId === c)
    } : null;
  });
}, Ve = (c) => {
  const r = g();
  return m(r, (t) => c ? x(c, t) : null);
}, je = (c) => {
  const r = g();
  return m(r, (t) => M(c, t));
}, U = () => {
  const c = g();
  return m(c, (r) => r.page.attributes);
}, V = () => {
  const c = g();
  return m(c, (r) => r.site.attributes);
}, ze = (c) => {
  const r = g(), t = D();
  return m(r, (d) => {
    if (!c)
      return console.log("useData: returning production data"), d.data;
    const e = {};
    for (const i of t) {
      const o = i.datasource.schema.items.examples;
      o && (e[i.alias] = o);
    }
    return e;
  });
}, Ne = (c) => D().find((t) => t.alias === c) ?? null;
function Ge(c) {
  const r = g(), t = m(r, (e) => e.getParentBrick);
  let d = c;
  for (; d; ) {
    const e = x(d, r.getState());
    if (e?.props.loop?.over)
      return e?.props.loop.over;
    d = t(d)?.id;
  }
  return null;
}
const D = () => {
  const c = g();
  return m(
    c,
    (r) => r.page.attributes.queries?.map((t) => {
      const d = r.site.attributes.queries?.find((i) => i.id === t.queryId);
      if (!d)
        return console.log(`WARN: Query with id ${t.queryId} not found in the store`), null;
      const e = r.site.datasources.find((i) => i.id === d.datasourceId);
      return e ? {
        ...t,
        queryInfo: d,
        datasource: e
      } : (console.warn(
        `Datasource with id ${d.datasourceId} not found for query ${t.queryId}`
      ), null);
    }).filter((t) => t !== null) ?? []
  );
}, He = () => {
  const c = g();
  return m(c, (r) => ({
    addAdditionalAssets: r.addAdditionalAssets,
    deleteBrick: r.deleteBrick,
    detachBrickFromContainer: r.detachBrickFromContainer,
    updateSiteAttributes: r.updateSiteAttributes,
    updatePageAttributes: r.updatePageAttributes,
    upsertQuery: r.upsertQuery,
    setSections: r.setSections,
    setThemes: r.setThemes,
    setTheme: r.setTheme,
    pickTheme: r.pickTheme,
    setSitemap: r.setSitemap,
    addPage: r.addPage,
    addDatasource: r.addDatasource,
    addDatarecord: r.addDatarecord,
    duplicateBrick: r.duplicateBrick,
    addBrick: r.addBrick,
    getBrick: r.getBrick,
    validatePreviewTheme: r.validatePreviewTheme,
    duplicateSection: r.duplicateSection,
    toggleBrickVisibilityPerBreakpoint: r.toggleBrickVisibility,
    getParentBrick: r.getParentBrick,
    updateBrickProps: r.updateBrickProps,
    moveBrick: r.moveBrick,
    reorderBrickWithin: r.reorderBrickWithin,
    getPositionWithinParent: r.getPositionWithinParent,
    canMoveTo: r.canMoveTo,
    moveBrickToContainerBrick: r.moveBrickToContainerBrick,
    moveBrickToSection: r.moveBrickToSection,
    deleteSection: r.deleteSection,
    moveSectionUp: r.moveSectionUp,
    moveSectionDown: r.moveSectionDown,
    reorderSections: r.reorderSections,
    updateSection: r.updateSection,
    updateSectionProps: r.updateSectionProps,
    addSection: r.addSection,
    createEmptySection: r.createEmptySection,
    isFirstSection: r.isFirstSection,
    isLastSection: r.isLastSection
  }));
}, Je = () => {
  const c = g();
  return m(c, (r) => r.page);
}, Ze = () => {
  const c = g();
  return m(c, (t) => t.site);
}, Ke = () => {
  const c = g();
  return m(c, (r) => r.site.theme);
}, Xe = () => {
  const c = g();
  return m(c, (r) => r.previewTheme);
}, Ye = (c) => {
  const r = g();
  $(() => r.subscribe((t) => t?.page.sections ?? [], E(c, 200), {
    fireImmediately: !1
  }), []);
}, _e = (c) => {
  const r = g();
  $(() => r.subscribe((t) => t.page.attributes, c, {
    equalityFn: w
  }), []);
}, ei = (c) => {
  const r = g();
  $(() => r.subscribe((t) => t.site.attributes, c, {
    equalityFn: w
  }), []);
}, ii = (c) => {
  const r = g();
  $(() => r.subscribe((t) => t.site.theme, c), []);
}, ri = () => {
  const c = g();
  return m(c, (r) => {
    const t = r.page.attributes.path, d = /:([a-zA-Z0-9_]+)/g;
    return Array.from(t.matchAll(d)).map((i) => i[0]);
  });
}, ni = () => {
  const c = g();
  return m(c, (r) => r.additionalAssets);
};
function j(c, r) {
  const { brick: t } = r.brickMap.get(c) ?? {};
  return t;
}
function M(c, r) {
  const t = r.brickMap.get(c);
  if (t)
    return r.page.sections.find((d) => d.id === t.sectionId);
}
function x(c, r) {
  for (const t of r.page.sections) {
    const d = t.bricks.find((o) => o.id === c);
    if (d)
      return d;
    const e = (o) => {
      for (const n of o) {
        if (n.id === c)
          return n;
        if (n.props?.$children) {
          const s = e(n.props.$children);
          if (s)
            return s;
        }
      }
    }, i = e(t.bricks);
    if (i)
      return i;
  }
  return null;
}
const oi = () => {
  const c = g();
  return m(c, (r) => r.site.sitePrompt);
};
function k(c) {
  const r = /* @__PURE__ */ new Map();
  function t(d, e, i) {
    r.set(d.id, { brick: d, sectionId: e, parentId: i }), d.props?.$children && d.props.$children.forEach((n) => t(n, e, d.id));
  }
  return c.forEach((d) => {
    d.bricks.forEach((e) => t(e, d.id, null));
  }), r;
}
export {
  R as DraftStoreContext,
  Ae as createDraftStore,
  ni as useAdditionalAssets,
  Ve as useBrick,
  ze as useData,
  qe as useDraft,
  He as useDraftHelpers,
  De as useDraftUndoManager,
  Ie as useGenerationState,
  Oe as useLastSaved,
  Ge as useLoopAlias,
  Ne as useLoopedQuery,
  Je as usePage,
  U as usePageAttributes,
  _e as usePageAttributesSubscribe,
  g as usePageContext,
  ri as usePagePathParams,
  D as usePageQueries,
  We as usePageVersion,
  Qe as useParentBrick,
  Xe as usePreviewTheme,
  Ue as useSection,
  je as useSectionByBrickId,
  Re as useSections,
  Ye as useSectionsSubscribe,
  Ze as useSite,
  V as useSiteAttributes,
  ei as useSiteAttributesSubscribe,
  oi as useSitePrompt,
  Fe as useSiteQueries,
  Le as useSiteQuery,
  ye as useSitemap,
  Ke as useTheme,
  ii as useThemeSubscribe,
  Ee as useThemes
};
//# sourceMappingURL=use-page-data.js.map
