import { g as Ke, av as qe, z as j, m as H } from "./chunk-3RDGTRSB.DySNCMmQ.js";
import "@sinclair/typebox";
import { debounce as Je, isEqual as Re } from "lodash-es";
import J, { createContext as Ye, useContext as Xe, useEffect as ee } from "react";
/*! Upstart.gg - Copyright (C) 2024 Flippable - https://github.com/upstart-gg/upstart/blob/main/LICENSE */
var we = "Invariant failed";
function L(e, t) {
  if (e) return;
  let n = typeof t == "function" ? t() : t, c = n ? `${we}: ${n}` : we;
  throw new Error(c);
}
var Fe = Symbol.for("immer-nothing"), Oe = Symbol.for("immer-draftable"), g = Symbol.for("immer-state"), Qe = process.env.NODE_ENV !== "production" ? [
  // All error codes, starting by 0:
  function(e) {
    return `The plugin for '${e}' has not been loaded into Immer. To enable the plugin, import and call \`enable${e}()\` when initializing your application.`;
  },
  function(e) {
    return `produce can only be called on things that are draftable: plain objects, arrays, Map, Set or classes that are marked with '[immerable]: true'. Got '${e}'`;
  },
  "This object has been frozen and should not be mutated",
  function(e) {
    return "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? " + e;
  },
  "An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.",
  "Immer forbids circular references",
  "The first or second argument to `produce` must be a function",
  "The third argument to `produce` must be a function or undefined",
  "First argument to `createDraft` must be a plain object, an array, or an immerable object",
  "First argument to `finishDraft` must be a draft returned by `createDraft`",
  function(e) {
    return `'current' expects a draft, got: ${e}`;
  },
  "Object.defineProperty() cannot be used on an Immer draft",
  "Object.setPrototypeOf() cannot be used on an Immer draft",
  "Immer only supports deleting array indices",
  "Immer only supports setting array indices and the 'length' property",
  function(e) {
    return `'original' expects a draft, got: ${e}`;
  }
  // Note: if more errors are added, the errorOffset in Patches.ts should be increased
  // See Patches.ts for additional errors
] : [];
function T(e, ...t) {
  if (process.env.NODE_ENV !== "production") {
    const n = Qe[e], c = typeof n == "function" ? n.apply(null, t) : n;
    throw new Error(`[Immer] ${c}`);
  }
  throw new Error(
    `[Immer] minified error nr: ${e}. Full error at: https://bit.ly/3cXEKWf`
  );
}
var R = Object.getPrototypeOf;
function F(e) {
  return !!e && !!e[g];
}
function $(e) {
  var t;
  return e ? Ne(e) || Array.isArray(e) || !!e[Oe] || !!((t = e.constructor) != null && t[Oe]) || re(e) || ne(e) : !1;
}
var Ze = Object.prototype.constructor.toString();
function Ne(e) {
  if (!e || typeof e != "object")
    return !1;
  const t = R(e);
  if (t === null)
    return !0;
  const n = Object.hasOwnProperty.call(t, "constructor") && t.constructor;
  return n === Object ? !0 : typeof n == "function" && Function.toString.call(n) === Ze;
}
function U(e, t) {
  te(e) === 0 ? Reflect.ownKeys(e).forEach((n) => {
    t(n, e[n], e);
  }) : e.forEach((n, c) => t(c, n, e));
}
function te(e) {
  const t = e[g];
  return t ? t.type_ : Array.isArray(e) ? 1 : re(e) ? 2 : ne(e) ? 3 : 0;
}
function _e(e, t) {
  return te(e) === 2 ? e.has(t) : Object.prototype.hasOwnProperty.call(e, t);
}
function We(e, t, n) {
  const c = te(e);
  c === 2 ? e.set(t, n) : c === 3 ? e.add(n) : e[t] = n;
}
function et(e, t) {
  return e === t ? e !== 0 || 1 / e === 1 / t : e !== e && t !== t;
}
function re(e) {
  return e instanceof Map;
}
function ne(e) {
  return e instanceof Set;
}
function O(e) {
  return e.copy_ || e.base_;
}
function Se(e, t) {
  if (re(e))
    return new Map(e);
  if (ne(e))
    return new Set(e);
  if (Array.isArray(e))
    return Array.prototype.slice.call(e);
  const n = Ne(e);
  if (t === !0 || t === "class_only" && !n) {
    const c = Object.getOwnPropertyDescriptors(e);
    delete c[g];
    let r = Reflect.ownKeys(c);
    for (let i = 0; i < r.length; i++) {
      const u = r[i], s = c[u];
      s.writable === !1 && (s.writable = !0, s.configurable = !0), (s.get || s.set) && (c[u] = {
        configurable: !0,
        writable: !0,
        // could live with !!desc.set as well here...
        enumerable: s.enumerable,
        value: e[u]
      });
    }
    return Object.create(R(e), c);
  } else {
    const c = R(e);
    if (c !== null && n)
      return { ...e };
    const r = Object.create(c);
    return Object.assign(r, e);
  }
}
function Ee(e, t = !1) {
  return ie(e) || F(e) || !$(e) || (te(e) > 1 && (e.set = e.add = e.clear = e.delete = tt), Object.freeze(e), t && Object.entries(e).forEach(([n, c]) => Ee(c, !0))), e;
}
function tt() {
  T(2);
}
function ie(e) {
  return Object.isFrozen(e);
}
var ve = {};
function z(e) {
  const t = ve[e];
  return t || T(0, e), t;
}
function rt(e, t) {
  ve[e] || (ve[e] = t);
}
var V;
function X() {
  return V;
}
function nt(e, t) {
  return {
    drafts_: [],
    parent_: e,
    immer_: t,
    // Whenever the modified draft contains a draft from another scope, we
    // need to prevent auto-freezing so the unowned draft can be finalized.
    canAutoFreeze_: !0,
    unfinalizedDrafts_: 0
  };
}
function Pe(e, t) {
  t && (z("Patches"), e.patches_ = [], e.inversePatches_ = [], e.patchListener_ = t);
}
function ye(e) {
  be(e), e.drafts_.forEach(it), e.drafts_ = null;
}
function be(e) {
  e === V && (V = e.parent_);
}
function Te(e) {
  return V = nt(V, e);
}
function it(e) {
  const t = e[g];
  t.type_ === 0 || t.type_ === 1 ? t.revoke_() : t.revoked_ = !0;
}
function De(e, t) {
  t.unfinalizedDrafts_ = t.drafts_.length;
  const n = t.drafts_[0];
  return e !== void 0 && e !== n ? (n[g].modified_ && (ye(t), T(4)), $(e) && (e = Q(t, e), t.parent_ || Z(t, e)), t.patches_ && z("Patches").generateReplacementPatches_(
    n[g].base_,
    e,
    t.patches_,
    t.inversePatches_
  )) : e = Q(t, n, []), ye(t), t.patches_ && t.patchListener_(t.patches_, t.inversePatches_), e !== Fe ? e : void 0;
}
function Q(e, t, n) {
  if (ie(t))
    return t;
  const c = t[g];
  if (!c)
    return U(
      t,
      (r, i) => Me(e, c, t, r, i, n)
    ), t;
  if (c.scope_ !== e)
    return t;
  if (!c.modified_)
    return Z(e, c.base_, !0), c.base_;
  if (!c.finalized_) {
    c.finalized_ = !0, c.scope_.unfinalizedDrafts_--;
    const r = c.copy_;
    let i = r, u = !1;
    c.type_ === 3 && (i = new Set(r), r.clear(), u = !0), U(
      i,
      (s, o) => Me(e, c, r, s, o, n, u)
    ), Z(e, r, !1), n && e.patches_ && z("Patches").generatePatches_(
      c,
      n,
      e.patches_,
      e.inversePatches_
    );
  }
  return c.copy_;
}
function Me(e, t, n, c, r, i, u) {
  if (process.env.NODE_ENV !== "production" && r === n && T(5), F(r)) {
    const s = i && t && t.type_ !== 3 && // Set objects are atomic since they have no keys.
    !_e(t.assigned_, c) ? i.concat(c) : void 0, o = Q(e, r, s);
    if (We(n, c, o), F(o))
      e.canAutoFreeze_ = !1;
    else
      return;
  } else u && n.add(r);
  if ($(r) && !ie(r)) {
    if (!e.immer_.autoFreeze_ && e.unfinalizedDrafts_ < 1)
      return;
    Q(e, r), (!t || !t.scope_.parent_) && typeof c != "symbol" && Object.prototype.propertyIsEnumerable.call(n, c) && Z(e, r);
  }
}
function Z(e, t, n = !1) {
  !e.parent_ && e.immer_.autoFreeze_ && e.canAutoFreeze_ && Ee(t, n);
}
function ot(e, t) {
  const n = Array.isArray(e), c = {
    type_: n ? 1 : 0,
    // Track which produce call this is associated with.
    scope_: t ? t.scope_ : X(),
    // True for both shallow and deep changes.
    modified_: !1,
    // Used during finalization.
    finalized_: !1,
    // Track which properties have been assigned (true) or deleted (false).
    assigned_: {},
    // The parent draft state.
    parent_: t,
    // The base state.
    base_: e,
    // The base proxy.
    draft_: null,
    // set below
    // The base copy with any updated values.
    copy_: null,
    // Called by the `produce` function.
    revoke_: null,
    isManual_: !1
  };
  let r = c, i = xe;
  n && (r = [c], i = G);
  const { revoke: u, proxy: s } = Proxy.revocable(r, i);
  return c.draft_ = s, c.revoke_ = u, s;
}
var xe = {
  get(e, t) {
    if (t === g)
      return e;
    const n = O(e);
    if (!_e(n, t))
      return ct(e, n, t);
    const c = n[t];
    return e.finalized_ || !$(c) ? c : c === de(e.base_, t) ? (le(e), e.copy_[t] = K(c, e)) : c;
  },
  has(e, t) {
    return t in O(e);
  },
  ownKeys(e) {
    return Reflect.ownKeys(O(e));
  },
  set(e, t, n) {
    const c = je(O(e), t);
    if (c != null && c.set)
      return c.set.call(e.draft_, n), !0;
    if (!e.modified_) {
      const r = de(O(e), t), i = r == null ? void 0 : r[g];
      if (i && i.base_ === n)
        return e.copy_[t] = n, e.assigned_[t] = !1, !0;
      if (et(n, r) && (n !== void 0 || _e(e.base_, t)))
        return !0;
      le(e), C(e);
    }
    return e.copy_[t] === n && // special case: handle new props with value 'undefined'
    (n !== void 0 || t in e.copy_) || // special case: NaN
    Number.isNaN(n) && Number.isNaN(e.copy_[t]) || (e.copy_[t] = n, e.assigned_[t] = !0), !0;
  },
  deleteProperty(e, t) {
    return de(e.base_, t) !== void 0 || t in e.base_ ? (e.assigned_[t] = !1, le(e), C(e)) : delete e.assigned_[t], e.copy_ && delete e.copy_[t], !0;
  },
  // Note: We never coerce `desc.value` into an Immer draft, because we can't make
  // the same guarantee in ES5 mode.
  getOwnPropertyDescriptor(e, t) {
    const n = O(e), c = Reflect.getOwnPropertyDescriptor(n, t);
    return c && {
      writable: !0,
      configurable: e.type_ !== 1 || t !== "length",
      enumerable: c.enumerable,
      value: n[t]
    };
  },
  defineProperty() {
    T(11);
  },
  getPrototypeOf(e) {
    return R(e.base_);
  },
  setPrototypeOf() {
    T(12);
  }
}, G = {};
U(xe, (e, t) => {
  G[e] = function() {
    return arguments[0] = arguments[0][0], t.apply(this, arguments);
  };
});
G.deleteProperty = function(e, t) {
  return process.env.NODE_ENV !== "production" && isNaN(parseInt(t)) && T(13), G.set.call(this, e, t, void 0);
};
G.set = function(e, t, n) {
  return process.env.NODE_ENV !== "production" && t !== "length" && isNaN(parseInt(t)) && T(14), xe.set.call(this, e[0], t, n, e[0]);
};
function de(e, t) {
  const n = e[g];
  return (n ? O(n) : e)[t];
}
function ct(e, t, n) {
  var r;
  const c = je(t, n);
  return c ? "value" in c ? c.value : (
    // This is a very special case, if the prop is a getter defined by the
    // prototype, we should invoke it with the draft as context!
    (r = c.get) == null ? void 0 : r.call(e.draft_)
  ) : void 0;
}
function je(e, t) {
  if (!(t in e))
    return;
  let n = R(e);
  for (; n; ) {
    const c = Object.getOwnPropertyDescriptor(n, t);
    if (c)
      return c;
    n = R(n);
  }
}
function C(e) {
  e.modified_ || (e.modified_ = !0, e.parent_ && C(e.parent_));
}
function le(e) {
  e.copy_ || (e.copy_ = Se(
    e.base_,
    e.scope_.immer_.useStrictShallowCopy_
  ));
}
var st = class {
  constructor(e) {
    this.autoFreeze_ = !0, this.useStrictShallowCopy_ = !1, this.produce = (t, n, c) => {
      if (typeof t == "function" && typeof n != "function") {
        const i = n;
        n = t;
        const u = this;
        return function(o = i, ...a) {
          return u.produce(o, (d) => n.call(this, d, ...a));
        };
      }
      typeof n != "function" && T(6), c !== void 0 && typeof c != "function" && T(7);
      let r;
      if ($(t)) {
        const i = Te(this), u = K(t, void 0);
        let s = !0;
        try {
          r = n(u), s = !1;
        } finally {
          s ? ye(i) : be(i);
        }
        return Pe(i, c), De(r, i);
      } else if (!t || typeof t != "object") {
        if (r = n(t), r === void 0 && (r = t), r === Fe && (r = void 0), this.autoFreeze_ && Ee(r, !0), c) {
          const i = [], u = [];
          z("Patches").generateReplacementPatches_(t, r, i, u), c(i, u);
        }
        return r;
      } else
        T(1, t);
    }, this.produceWithPatches = (t, n) => {
      if (typeof t == "function")
        return (u, ...s) => this.produceWithPatches(u, (o) => t(o, ...s));
      let c, r;
      return [this.produce(t, n, (u, s) => {
        c = u, r = s;
      }), c, r];
    }, typeof (e == null ? void 0 : e.autoFreeze) == "boolean" && this.setAutoFreeze(e.autoFreeze), typeof (e == null ? void 0 : e.useStrictShallowCopy) == "boolean" && this.setUseStrictShallowCopy(e.useStrictShallowCopy);
  }
  createDraft(e) {
    $(e) || T(8), F(e) && (e = at(e));
    const t = Te(this), n = K(e, void 0);
    return n[g].isManual_ = !0, be(t), n;
  }
  finishDraft(e, t) {
    const n = e && e[g];
    (!n || !n.isManual_) && T(9);
    const { scope_: c } = n;
    return Pe(c, t), De(void 0, c);
  }
  /**
   * Pass true to automatically freeze all copies created by Immer.
   *
   * By default, auto-freezing is enabled.
   */
  setAutoFreeze(e) {
    this.autoFreeze_ = e;
  }
  /**
   * Pass true to enable strict shallow copy.
   *
   * By default, immer does not copy the object descriptors such as getter, setter and non-enumrable properties.
   */
  setUseStrictShallowCopy(e) {
    this.useStrictShallowCopy_ = e;
  }
  applyPatches(e, t) {
    let n;
    for (n = t.length - 1; n >= 0; n--) {
      const r = t[n];
      if (r.path.length === 0 && r.op === "replace") {
        e = r.value;
        break;
      }
    }
    n > -1 && (t = t.slice(n + 1));
    const c = z("Patches").applyPatches_;
    return F(e) ? c(e, t) : this.produce(
      e,
      (r) => c(r, t)
    );
  }
};
function K(e, t) {
  const n = re(e) ? z("MapSet").proxyMap_(e, t) : ne(e) ? z("MapSet").proxySet_(e, t) : ot(e, t);
  return (t ? t.scope_ : X()).drafts_.push(n), n;
}
function at(e) {
  return F(e) || T(10, e), He(e);
}
function He(e) {
  if (!$(e) || ie(e))
    return e;
  const t = e[g];
  let n;
  if (t) {
    if (!t.modified_)
      return t.base_;
    t.finalized_ = !0, n = Se(e, t.scope_.immer_.useStrictShallowCopy_);
  } else
    n = Se(e, !0);
  return U(n, (c, r) => {
    We(n, c, He(r));
  }), t && (t.finalized_ = !1), n;
}
function ut() {
  class e extends Map {
    constructor(o, a) {
      super(), this[g] = {
        type_: 2,
        parent_: a,
        scope_: a ? a.scope_ : X(),
        modified_: !1,
        finalized_: !1,
        copy_: void 0,
        assigned_: void 0,
        base_: o,
        draft_: this,
        isManual_: !1,
        revoked_: !1
      };
    }
    get size() {
      return O(this[g]).size;
    }
    has(o) {
      return O(this[g]).has(o);
    }
    set(o, a) {
      const d = this[g];
      return u(d), (!O(d).has(o) || O(d).get(o) !== a) && (n(d), C(d), d.assigned_.set(o, !0), d.copy_.set(o, a), d.assigned_.set(o, !0)), this;
    }
    delete(o) {
      if (!this.has(o))
        return !1;
      const a = this[g];
      return u(a), n(a), C(a), a.base_.has(o) ? a.assigned_.set(o, !1) : a.assigned_.delete(o), a.copy_.delete(o), !0;
    }
    clear() {
      const o = this[g];
      u(o), O(o).size && (n(o), C(o), o.assigned_ = /* @__PURE__ */ new Map(), U(o.base_, (a) => {
        o.assigned_.set(a, !1);
      }), o.copy_.clear());
    }
    forEach(o, a) {
      const d = this[g];
      O(d).forEach((l, f, p) => {
        o.call(a, this.get(f), f, this);
      });
    }
    get(o) {
      const a = this[g];
      u(a);
      const d = O(a).get(o);
      if (a.finalized_ || !$(d) || d !== a.base_.get(o))
        return d;
      const l = K(d, a);
      return n(a), a.copy_.set(o, l), l;
    }
    keys() {
      return O(this[g]).keys();
    }
    values() {
      const o = this.keys();
      return {
        [Symbol.iterator]: () => this.values(),
        next: () => {
          const a = o.next();
          return a.done ? a : {
            done: !1,
            value: this.get(a.value)
          };
        }
      };
    }
    entries() {
      const o = this.keys();
      return {
        [Symbol.iterator]: () => this.entries(),
        next: () => {
          const a = o.next();
          if (a.done)
            return a;
          const d = this.get(a.value);
          return {
            done: !1,
            value: [a.value, d]
          };
        }
      };
    }
    [Symbol.iterator]() {
      return this.entries();
    }
  }
  function t(s, o) {
    return new e(s, o);
  }
  function n(s) {
    s.copy_ || (s.assigned_ = /* @__PURE__ */ new Map(), s.copy_ = new Map(s.base_));
  }
  class c extends Set {
    constructor(o, a) {
      super(), this[g] = {
        type_: 3,
        parent_: a,
        scope_: a ? a.scope_ : X(),
        modified_: !1,
        finalized_: !1,
        copy_: void 0,
        base_: o,
        draft_: this,
        drafts_: /* @__PURE__ */ new Map(),
        revoked_: !1,
        isManual_: !1
      };
    }
    get size() {
      return O(this[g]).size;
    }
    has(o) {
      const a = this[g];
      return u(a), a.copy_ ? !!(a.copy_.has(o) || a.drafts_.has(o) && a.copy_.has(a.drafts_.get(o))) : a.base_.has(o);
    }
    add(o) {
      const a = this[g];
      return u(a), this.has(o) || (i(a), C(a), a.copy_.add(o)), this;
    }
    delete(o) {
      if (!this.has(o))
        return !1;
      const a = this[g];
      return u(a), i(a), C(a), a.copy_.delete(o) || (a.drafts_.has(o) ? a.copy_.delete(a.drafts_.get(o)) : (
        /* istanbul ignore next */
        !1
      ));
    }
    clear() {
      const o = this[g];
      u(o), O(o).size && (i(o), C(o), o.copy_.clear());
    }
    values() {
      const o = this[g];
      return u(o), i(o), o.copy_.values();
    }
    entries() {
      const o = this[g];
      return u(o), i(o), o.copy_.entries();
    }
    keys() {
      return this.values();
    }
    [Symbol.iterator]() {
      return this.values();
    }
    forEach(o, a) {
      const d = this.values();
      let l = d.next();
      for (; !l.done; )
        o.call(a, l.value, l.value, this), l = d.next();
    }
  }
  function r(s, o) {
    return new c(s, o);
  }
  function i(s) {
    s.copy_ || (s.copy_ = /* @__PURE__ */ new Set(), s.base_.forEach((o) => {
      if ($(o)) {
        const a = K(o, s);
        s.drafts_.set(o, a), s.copy_.add(a);
      } else
        s.copy_.add(o);
    }));
  }
  function u(s) {
    s.revoked_ && T(3, JSON.stringify(O(s)));
  }
  rt("MapSet", { proxyMap_: t, proxySet_: r });
}
var M = new st(), dt = M.produce;
M.produceWithPatches.bind(
  M
);
M.setAutoFreeze.bind(M);
M.setUseStrictShallowCopy.bind(M);
M.applyPatches.bind(M);
M.createDraft.bind(M);
M.finishDraft.bind(M);
const lt = {}, Be = (e) => {
  let t;
  const n = /* @__PURE__ */ new Set(), c = (d, l) => {
    const f = typeof d == "function" ? d(t) : d;
    if (!Object.is(f, t)) {
      const p = t;
      t = l ?? (typeof f != "object" || f === null) ? f : Object.assign({}, t, f), n.forEach((m) => m(t, p));
    }
  }, r = () => t, o = { setState: c, getState: r, getInitialState: () => a, subscribe: (d) => (n.add(d), () => n.delete(d)), destroy: () => {
    (lt ? "production" : void 0) !== "production" && console.warn(
      "[DEPRECATED] The `destroy` method will be unsupported in a future version. Instead use unsubscribe function returned by subscribe. Everything will be garbage-collected if store is garbage-collected."
    ), n.clear();
  } }, a = t = e(c, r, o);
  return o;
}, Ue = (e) => e ? Be(e) : Be;
var ge = { exports: {} }, fe = {}, Y = { exports: {} }, pe = {};
/**
 * @license React
 * use-sync-external-store-shim.production.min.js
 *
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */
var Ie;
function ft() {
  if (Ie) return pe;
  Ie = 1;
  var e = J;
  function t(l, f) {
    return l === f && (l !== 0 || 1 / l === 1 / f) || l !== l && f !== f;
  }
  var n = typeof Object.is == "function" ? Object.is : t, c = e.useState, r = e.useEffect, i = e.useLayoutEffect, u = e.useDebugValue;
  function s(l, f) {
    var p = f(), m = c({ inst: { value: p, getSnapshot: f } }), h = m[0].inst, _ = m[1];
    return i(function() {
      h.value = p, h.getSnapshot = f, o(h) && _({ inst: h });
    }, [l, p, f]), r(function() {
      return o(h) && _({ inst: h }), l(function() {
        o(h) && _({ inst: h });
      });
    }, [l]), u(p), p;
  }
  function o(l) {
    var f = l.getSnapshot;
    l = l.value;
    try {
      var p = f();
      return !n(l, p);
    } catch {
      return !0;
    }
  }
  function a(l, f) {
    return f();
  }
  var d = typeof window > "u" || typeof window.document > "u" || typeof window.document.createElement > "u" ? a : s;
  return pe.useSyncExternalStore = e.useSyncExternalStore !== void 0 ? e.useSyncExternalStore : d, pe;
}
var he = {};
/**
 * @license React
 * use-sync-external-store-shim.development.js
 *
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */
var Ce;
function pt() {
  return Ce || (Ce = 1, process.env.NODE_ENV !== "production" && function() {
    typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ < "u" && typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart == "function" && __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(new Error());
    var e = J, t = e.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
    function n(v) {
      {
        for (var y = arguments.length, x = new Array(y > 1 ? y - 1 : 0), k = 1; k < y; k++)
          x[k - 1] = arguments[k];
        c("error", v, x);
      }
    }
    function c(v, y, x) {
      {
        var k = t.ReactDebugCurrentFrame, P = k.getStackAddendum();
        P !== "" && (y += "%s", x = x.concat([P]));
        var B = x.map(function(D) {
          return String(D);
        });
        B.unshift("Warning: " + y), Function.prototype.apply.call(console[v], console, B);
      }
    }
    function r(v, y) {
      return v === y && (v !== 0 || 1 / v === 1 / y) || v !== v && y !== y;
    }
    var i = typeof Object.is == "function" ? Object.is : r, u = e.useState, s = e.useEffect, o = e.useLayoutEffect, a = e.useDebugValue, d = !1, l = !1;
    function f(v, y, x) {
      d || e.startTransition !== void 0 && (d = !0, n("You are using an outdated, pre-release alpha of React 18 that does not support useSyncExternalStore. The use-sync-external-store shim will not work correctly. Upgrade to a newer pre-release."));
      var k = y();
      if (!l) {
        var P = y();
        i(k, P) || (n("The result of getSnapshot should be cached to avoid an infinite loop"), l = !0);
      }
      var B = u({
        inst: {
          value: k,
          getSnapshot: y
        }
      }), D = B[0].inst, N = B[1];
      return o(function() {
        D.value = k, D.getSnapshot = y, p(D) && N({
          inst: D
        });
      }, [v, k, y]), s(function() {
        p(D) && N({
          inst: D
        });
        var oe = function() {
          p(D) && N({
            inst: D
          });
        };
        return v(oe);
      }, [v]), a(k), k;
    }
    function p(v) {
      var y = v.getSnapshot, x = v.value;
      try {
        var k = y();
        return !i(x, k);
      } catch {
        return !0;
      }
    }
    function m(v, y, x) {
      return y();
    }
    var h = typeof window < "u" && typeof window.document < "u" && typeof window.document.createElement < "u", _ = !h, b = _ ? m : f, S = e.useSyncExternalStore !== void 0 ? e.useSyncExternalStore : b;
    he.useSyncExternalStore = S, typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ < "u" && typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop == "function" && __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(new Error());
  }()), he;
}
var $e;
function Ve() {
  return $e || ($e = 1, process.env.NODE_ENV === "production" ? Y.exports = ft() : Y.exports = pt()), Y.exports;
}
/**
 * @license React
 * use-sync-external-store-shim/with-selector.production.min.js
 *
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */
var Ae;
function ht() {
  if (Ae) return fe;
  Ae = 1;
  var e = J, t = Ve();
  function n(a, d) {
    return a === d && (a !== 0 || 1 / a === 1 / d) || a !== a && d !== d;
  }
  var c = typeof Object.is == "function" ? Object.is : n, r = t.useSyncExternalStore, i = e.useRef, u = e.useEffect, s = e.useMemo, o = e.useDebugValue;
  return fe.useSyncExternalStoreWithSelector = function(a, d, l, f, p) {
    var m = i(null);
    if (m.current === null) {
      var h = { hasValue: !1, value: null };
      m.current = h;
    } else h = m.current;
    m = s(function() {
      function b(k) {
        if (!S) {
          if (S = !0, v = k, k = f(k), p !== void 0 && h.hasValue) {
            var P = h.value;
            if (p(P, k)) return y = P;
          }
          return y = k;
        }
        if (P = y, c(v, k)) return P;
        var B = f(k);
        return p !== void 0 && p(P, B) ? P : (v = k, y = B);
      }
      var S = !1, v, y, x = l === void 0 ? null : l;
      return [function() {
        return b(d());
      }, x === null ? void 0 : function() {
        return b(x());
      }];
    }, [d, l, f, p]);
    var _ = r(a, m[0], m[1]);
    return u(function() {
      h.hasValue = !0, h.value = _;
    }, [_]), o(_), _;
  }, fe;
}
var me = {};
/**
 * @license React
 * use-sync-external-store-shim/with-selector.development.js
 *
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */
var ze;
function mt() {
  return ze || (ze = 1, process.env.NODE_ENV !== "production" && function() {
    typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ < "u" && typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart == "function" && __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(new Error());
    var e = J, t = Ve();
    function n(d, l) {
      return d === l && (d !== 0 || 1 / d === 1 / l) || d !== d && l !== l;
    }
    var c = typeof Object.is == "function" ? Object.is : n, r = t.useSyncExternalStore, i = e.useRef, u = e.useEffect, s = e.useMemo, o = e.useDebugValue;
    function a(d, l, f, p, m) {
      var h = i(null), _;
      h.current === null ? (_ = {
        hasValue: !1,
        value: null
      }, h.current = _) : _ = h.current;
      var b = s(function() {
        var x = !1, k, P, B = function(W) {
          if (!x) {
            x = !0, k = W;
            var ce = p(W);
            if (m !== void 0 && _.hasValue) {
              var se = _.value;
              if (m(se, ce))
                return P = se, se;
            }
            return P = ce, ce;
          }
          var Ge = k, ae = P;
          if (c(Ge, W))
            return ae;
          var ue = p(W);
          return m !== void 0 && m(ae, ue) ? ae : (k = W, P = ue, ue);
        }, D = f === void 0 ? null : f, N = function() {
          return B(l());
        }, oe = D === null ? void 0 : function() {
          return B(D());
        };
        return [N, oe];
      }, [l, f, p, m]), S = b[0], v = b[1], y = r(d, S, v);
      return u(function() {
        _.hasValue = !0, _.value = y;
      }, [y]), o(y), y;
    }
    me.useSyncExternalStoreWithSelector = a, typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ < "u" && typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop == "function" && __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(new Error());
  }()), me;
}
process.env.NODE_ENV === "production" ? ge.exports = ht() : ge.exports = mt();
var _t = ge.exports;
const St = /* @__PURE__ */ Ke(_t), { useDebugValue: vt } = J, { useSyncExternalStoreWithSelector: yt } = St, bt = (e) => e;
function w(e, t = bt, n) {
  const c = yt(
    e.subscribe,
    e.getState,
    e.getServerState || e.getInitialState,
    t,
    n
  );
  return vt(c), c;
}
var Le = (e, t, n) => (r, i) => ({
  pastStates: (n == null ? void 0 : n.pastStates) || [],
  futureStates: (n == null ? void 0 : n.futureStates) || [],
  undo: (u = 1) => {
    var s, o;
    if (i().pastStates.length) {
      const a = ((s = n == null ? void 0 : n.partialize) == null ? void 0 : s.call(n, t())) || t(), d = i().pastStates.splice(-u, u), l = d.shift();
      e(l), r({
        pastStates: i().pastStates,
        futureStates: i().futureStates.concat(
          ((o = n == null ? void 0 : n.diff) == null ? void 0 : o.call(n, a, l)) || a,
          d.reverse()
        )
      });
    }
  },
  redo: (u = 1) => {
    var s, o;
    if (i().futureStates.length) {
      const a = ((s = n == null ? void 0 : n.partialize) == null ? void 0 : s.call(n, t())) || t(), d = i().futureStates.splice(-u, u), l = d.shift();
      e(l), r({
        pastStates: i().pastStates.concat(
          ((o = n == null ? void 0 : n.diff) == null ? void 0 : o.call(n, a, l)) || a,
          d.reverse()
        ),
        futureStates: i().futureStates
      });
    }
  },
  clear: () => r({ pastStates: [], futureStates: [] }),
  isTracking: !0,
  pause: () => r({ isTracking: !1 }),
  resume: () => r({ isTracking: !0 }),
  setOnSave: (u) => r({ _onSave: u }),
  // Internal properties
  _onSave: n == null ? void 0 : n.onSave,
  _handleSet: (u, s, o, a) => {
    var d, l;
    n != null && n.limit && i().pastStates.length >= (n == null ? void 0 : n.limit) && i().pastStates.shift(), (l = (d = i())._onSave) == null || l.call(d, u, o), r({
      pastStates: i().pastStates.concat(a || u),
      futureStates: []
    });
  }
}), gt = (e, t) => (c, r, i) => {
  var a, d;
  i.temporal = Ue(
    ((a = t == null ? void 0 : t.wrapTemporal) == null ? void 0 : a.call(t, Le(c, r, t))) || Le(c, r, t)
  );
  const u = ((d = t == null ? void 0 : t.handleSet) == null ? void 0 : d.call(
    t,
    i.temporal.getState()._handleSet
  )) || i.temporal.getState()._handleSet, s = (l) => {
    var m, h, _;
    if (!i.temporal.getState().isTracking) return;
    const f = ((m = t == null ? void 0 : t.partialize) == null ? void 0 : m.call(t, r())) || r(), p = (h = t == null ? void 0 : t.diff) == null ? void 0 : h.call(t, l, f);
    // If the user has provided a diff function but nothing has been changed, deltaState will be null
    p === null || // If the user has provided an equality function, use it
    (_ = t == null ? void 0 : t.equality) != null && _.call(t, l, f) || u(
      l,
      void 0,
      f,
      p
    );
  }, o = i.setState;
  return i.setState = (...l) => {
    var p;
    const f = ((p = t == null ? void 0 : t.partialize) == null ? void 0 : p.call(t, r())) || r();
    o(...l), s(f);
  }, e(
    // Modify the set function to call the userlandSet function
    (...l) => {
      var p;
      const f = ((p = t == null ? void 0 : t.partialize) == null ? void 0 : p.call(t, r())) || r();
      c(...l), s(f);
    },
    r,
    i
  );
};
const kt = {}, Et = (e) => (t, n, c) => {
  const r = c.subscribe;
  return c.subscribe = (u, s, o) => {
    let a = u;
    if (s) {
      const d = (o == null ? void 0 : o.equalityFn) || Object.is;
      let l = u(c.getState());
      a = (f) => {
        const p = u(f);
        if (!d(l, p)) {
          const m = l;
          s(l = p, m);
        }
      }, o != null && o.fireImmediately && s(l, l);
    }
    return r(a);
  }, e(t, n, c);
}, xt = Et;
function wt(e, t) {
  let n;
  try {
    n = e();
  } catch {
    return;
  }
  return {
    getItem: (r) => {
      var i;
      const u = (o) => o === null ? null : JSON.parse(o, void 0), s = (i = n.getItem(r)) != null ? i : null;
      return s instanceof Promise ? s.then(u) : u(s);
    },
    setItem: (r, i) => n.setItem(
      r,
      JSON.stringify(i, void 0)
    ),
    removeItem: (r) => n.removeItem(r)
  };
}
const q = (e) => (t) => {
  try {
    const n = e(t);
    return n instanceof Promise ? n : {
      then(c) {
        return q(c)(n);
      },
      catch(c) {
        return this;
      }
    };
  } catch (n) {
    return {
      then(c) {
        return this;
      },
      catch(c) {
        return q(c)(n);
      }
    };
  }
}, Ot = (e, t) => (n, c, r) => {
  let i = {
    getStorage: () => localStorage,
    serialize: JSON.stringify,
    deserialize: JSON.parse,
    partialize: (_) => _,
    version: 0,
    merge: (_, b) => ({
      ...b,
      ..._
    }),
    ...t
  }, u = !1;
  const s = /* @__PURE__ */ new Set(), o = /* @__PURE__ */ new Set();
  let a;
  try {
    a = i.getStorage();
  } catch {
  }
  if (!a)
    return e(
      (..._) => {
        console.warn(
          `[zustand persist middleware] Unable to update item '${i.name}', the given storage is currently unavailable.`
        ), n(..._);
      },
      c,
      r
    );
  const d = q(i.serialize), l = () => {
    const _ = i.partialize({ ...c() });
    let b;
    const S = d({ state: _, version: i.version }).then(
      (v) => a.setItem(i.name, v)
    ).catch((v) => {
      b = v;
    });
    if (b)
      throw b;
    return S;
  }, f = r.setState;
  r.setState = (_, b) => {
    f(_, b), l();
  };
  const p = e(
    (..._) => {
      n(..._), l();
    },
    c,
    r
  );
  let m;
  const h = () => {
    var _;
    if (!a) return;
    u = !1, s.forEach((S) => S(c()));
    const b = ((_ = i.onRehydrateStorage) == null ? void 0 : _.call(i, c())) || void 0;
    return q(a.getItem.bind(a))(i.name).then((S) => {
      if (S)
        return i.deserialize(S);
    }).then((S) => {
      if (S)
        if (typeof S.version == "number" && S.version !== i.version) {
          if (i.migrate)
            return i.migrate(
              S.state,
              S.version
            );
          console.error(
            "State loaded from storage couldn't be migrated since no migrate function was provided"
          );
        } else
          return S.state;
    }).then((S) => {
      var v;
      return m = i.merge(
        S,
        (v = c()) != null ? v : p
      ), n(m, !0), l();
    }).then(() => {
      b == null || b(m, void 0), u = !0, o.forEach((S) => S(m));
    }).catch((S) => {
      b == null || b(void 0, S);
    });
  };
  return r.persist = {
    setOptions: (_) => {
      i = {
        ...i,
        ..._
      }, _.getStorage && (a = _.getStorage());
    },
    clearStorage: () => {
      a == null || a.removeItem(i.name);
    },
    getOptions: () => i,
    rehydrate: () => h(),
    hasHydrated: () => u,
    onHydrate: (_) => (s.add(_), () => {
      s.delete(_);
    }),
    onFinishHydration: (_) => (o.add(_), () => {
      o.delete(_);
    })
  }, h(), m || p;
}, Pt = (e, t) => (n, c, r) => {
  let i = {
    storage: wt(() => localStorage),
    partialize: (h) => h,
    version: 0,
    merge: (h, _) => ({
      ..._,
      ...h
    }),
    ...t
  }, u = !1;
  const s = /* @__PURE__ */ new Set(), o = /* @__PURE__ */ new Set();
  let a = i.storage;
  if (!a)
    return e(
      (...h) => {
        console.warn(
          `[zustand persist middleware] Unable to update item '${i.name}', the given storage is currently unavailable.`
        ), n(...h);
      },
      c,
      r
    );
  const d = () => {
    const h = i.partialize({ ...c() });
    return a.setItem(i.name, {
      state: h,
      version: i.version
    });
  }, l = r.setState;
  r.setState = (h, _) => {
    l(h, _), d();
  };
  const f = e(
    (...h) => {
      n(...h), d();
    },
    c,
    r
  );
  r.getInitialState = () => f;
  let p;
  const m = () => {
    var h, _;
    if (!a) return;
    u = !1, s.forEach((S) => {
      var v;
      return S((v = c()) != null ? v : f);
    });
    const b = ((_ = i.onRehydrateStorage) == null ? void 0 : _.call(i, (h = c()) != null ? h : f)) || void 0;
    return q(a.getItem.bind(a))(i.name).then((S) => {
      if (S)
        if (typeof S.version == "number" && S.version !== i.version) {
          if (i.migrate)
            return [
              !0,
              i.migrate(
                S.state,
                S.version
              )
            ];
          console.error(
            "State loaded from storage couldn't be migrated since no migrate function was provided"
          );
        } else
          return [!1, S.state];
      return [!1, void 0];
    }).then((S) => {
      var v;
      const [y, x] = S;
      if (p = i.merge(
        x,
        (v = c()) != null ? v : f
      ), n(p, !0), y)
        return d();
    }).then(() => {
      b == null || b(p, void 0), p = c(), u = !0, o.forEach((S) => S(p));
    }).catch((S) => {
      b == null || b(void 0, S);
    });
  };
  return r.persist = {
    setOptions: (h) => {
      i = {
        ...i,
        ...h
      }, h.storage && (a = h.storage);
    },
    clearStorage: () => {
      a == null || a.removeItem(i.name);
    },
    getOptions: () => i,
    rehydrate: () => m(),
    hasHydrated: () => u,
    onHydrate: (h) => (s.add(h), () => {
      s.delete(h);
    }),
    onFinishHydration: (h) => (o.add(h), () => {
      o.delete(h);
    })
  }, i.skipHydration || m(), p || f;
}, Tt = (e, t) => "getStorage" in t || "serialize" in t || "deserialize" in t ? ((kt ? "production" : void 0) !== "production" && console.warn(
  "[DEPRECATED] `getStorage`, `serialize` and `deserialize` options are deprecated. Use `storage` option instead."
), Ot(e, t)) : Pt(e, t), Rt = Tt, Dt = (e) => (t, n, c) => (c.setState = (r, i, ...u) => {
  const s = typeof r == "function" ? dt(r) : r;
  return t(s, i, ...u);
}, e(c.setState, n, c)), Mt = Dt;
ut();
const Ft = (e) => {
  const t = {
    data: {},
    mode: "local",
    brickMap: A(e.sections),
    themes: [],
    pages: [],
    datasources: [],
    datarecords: [],
    sitePrompt: ""
    // themes: [defaultTheme, { ...defaultTheme, id: "t2" }, { ...defaultTheme, id: "t3" }],
  };
  return Ue()(
    xt(
      gt(
        Mt((n, c) => ({
          ...t,
          ...e,
          detachBrickFromContainer: (r) => n((i) => {
            var p;
            const u = i.brickMap.get(r);
            L(u, `Cannot detach brick ${r}, it does not exist in the brickMap`);
            const { brick: s, sectionId: o, parentId: a } = u;
            if (!a) {
              console.warn("Cannot detach brick %s, it is not in a container", r);
              return;
            }
            const d = I(a, i);
            if (!d) {
              console.error("Cannot detach brick %s, its parent %s does not exist", r, a);
              return;
            }
            const l = i.sections.find((m) => m.id === o);
            if (!l) {
              console.error("Cannot detach brick %s, its parent section %s does not exist", r, o);
              return;
            }
            d.props.$children = (p = d.props.$children) == null ? void 0 : p.filter(
              (m) => m.id !== r
            );
            const f = l.bricks.findIndex((m) => m.id === a);
            l.bricks.splice(f + 1, 0, {
              ...s
            }), i.brickMap = A(i.sections);
          }),
          createEmptySection: (r, i) => n((u) => {
            var d;
            const s = u.sections.length, o = 1 + (i ? ((d = u.sections.find((l) => l.id === i)) == null ? void 0 : d.order) ?? s : s), a = {
              id: r,
              order: o,
              label: `Section ${s + 1}`,
              bricks: [],
              props: {}
            };
            u.sections.forEach((l) => {
              l.order >= o && (l.order += 1);
            }), u.sections.push(a);
          }),
          addPage: (r) => n((i) => {
            i.pages.push(r), i.id === "_default_" && (i.id = r.id, i.path = r.path, i.label = r.label, i.sections = r.sections, r.attr && (i.attr = r.attr));
          }),
          isFirstSection: (r) => {
            var u;
            return ((u = c().sections.find((s) => s.order === 0)) == null ? void 0 : u.id) === r;
          },
          setSections: (r) => n((i) => {
            i.sections = r, i.brickMap = A(r);
          }),
          pickTheme: (r) => n((i) => {
            const u = i.themes.find((s) => s.id === r);
            if (!u) {
              console.error("Cannot pick theme %s, it does not exist", r);
              return;
            }
            i.theme = u;
          }),
          setThemes: (r) => n((i) => {
            i.themes = r;
          }),
          setSitemap: (r) => n((i) => {
            i.sitemap = r;
          }),
          getPageDataForDuplication: () => {
            const r = c(), i = r.sitemap.length + 1;
            return console.log("state.sitemap", r.sitemap), {
              id: `page-${H()}`,
              label: `${r.label} (page ${i})`,
              path: `${r.path}-${i}`,
              sections: r.sections,
              attr: r.attr,
              attributes: r.attributes,
              datasources: r.datasources,
              datarecords: r.datarecords
            };
          },
          isLastSection: (r) => {
            var u;
            const i = c();
            return ((u = i.sections.find((s) => s.order === i.sections.length - 1)) == null ? void 0 : u.id) === r;
          },
          addDatasource: (r) => n((i) => {
            if (i.datasources.find((s) => s.id === r.id)) {
              console.error("Cannot add datasource %s, it already exists", r.id);
              return;
            }
            i.datasources.push(r);
          }),
          addDatarecord: (r) => n((i) => {
            if (i.datarecords.find((s) => s.id === r.id)) {
              console.error("Cannot add datarecord %s, it already exists", r.id);
              return;
            }
            i.datarecords.push(r);
          }),
          addSection: (r) => n((i) => {
            i.sections.push(r);
          }),
          updateSection: (r, i) => n((u) => {
            const s = u.sections.findIndex((o) => o.id === r);
            s !== -1 && (u.sections[s] = {
              ...u.sections[s],
              ...i
            });
          }),
          duplicateSection: (r) => n((i) => {
            const u = i.sections.find((a) => a.id === r);
            if (!u) {
              console.error("Cannot duplicate section %s, it does not exist", r);
              return;
            }
            const s = i.sections.findIndex((a) => a.id === r), o = {
              ...u,
              id: `s_${H()}`,
              order: u.order + 1,
              // increment order to place it after the original
              label: `${u.label} (copy)`,
              // generate new bricks with new IDs
              bricks: u.bricks.map((a) => {
                var d;
                return {
                  ...a,
                  id: `b_${H()}`,
                  props: j({}, a.props, {
                    $children: (d = a.props.$children) == null ? void 0 : d.map((l) => ({
                      ...l,
                      id: `b_${H()}`
                    }))
                  })
                };
              })
            };
            i.sections.splice(s + 1, 0, o), i.brickMap = A(i.sections);
          }),
          deleteSection: (r) => n((i) => {
            if (!i.sections.find((s) => s.id === r))
              return console.warn("Cannot delete section %s: not found", r), null;
            i.brickMap.forEach((s) => {
              s.sectionId === r && i.brickMap.delete(s.brick.id);
            }), i.sections = i.sections.filter((s) => s.id !== r);
          }),
          getSection: (r) => c().sections.find((i) => i.id === r),
          moveSectionUp: (r) => n((i) => {
            const u = i.sections.find((a) => a.id === r);
            i.sections.findIndex((a) => a.id === r), L(u, "Section not found");
            const s = i.sections.find((a) => a.order === u.order - 1);
            i.sections.findIndex((a) => a.order === u.order - 1), L(s, "Previous section not found");
            const o = u.order;
            u.order = s.order, s.order = o;
          }),
          moveSectionDown: (r) => n((i) => {
            const u = i.sections.find((f) => f.id === r), s = i.sections.findIndex((f) => f.id === r);
            L(u, "Section not found");
            const o = i.sections.find((f) => f.order === u.order + 1), a = i.sections.findIndex((f) => f.order === u.order + 1);
            L(o, "Next section not found");
            const d = u.order, l = o.order;
            i.sections[s].order = l, i.sections[a].order = d;
          }),
          reorderSections: (r) => n((i) => {
            const u = [];
            let s = 0;
            r.forEach((o) => {
              const a = i.sections.find((d) => d.id === o);
              a && u.push({ ...a, order: ++s });
            }), i.sections.forEach((o) => {
              r.includes(o.id) || u.push({ ...o, order: ++s });
            }), i.sections = u, i.brickMap = A(i.sections);
          }),
          setLastLoaded: () => n((r) => {
            r.lastLoaded = /* @__PURE__ */ new Date();
          }),
          deleteBrick: (r) => n((i) => {
            const u = i.brickMap.get(r);
            if (!u) {
              console.error("Cannot delete brick %s, it does not exist", r);
              return;
            }
            const s = [], o = (f) => {
              var m;
              const p = i.brickMap.get(f);
              (m = p == null ? void 0 : p.brick.props) != null && m.$children && p.brick.props.$children.forEach((_) => {
                s.push(_.id), o(_.id);
              });
            };
            o(r);
            const { sectionId: a, parentId: d } = u, l = i.sections.find((f) => f.id === a);
            if (d) {
              const f = I(d, i);
              if (f != null && f.props.$children) {
                const p = f.props.$children.findIndex((m) => m.id === r);
                f.props.$children.splice(p, 1);
              }
            } else l && (l.bricks = l.bricks.filter((f) => f.id !== r));
            i.brickMap.delete(r), s.forEach((f) => {
              i.brickMap.delete(f);
            });
          }),
          duplicateBrick: (r) => n((i) => {
            const u = i.brickMap.get(r);
            if (!u) {
              console.error("Cannot duplicate brick %s, it does not exist", r);
              return;
            }
            const s = (f) => {
              const p = `brick-${H()}`, m = {
                ...f,
                id: p,
                props: { ...f.props },
                mobileProps: f.mobileProps ? { ...f.mobileProps } : void 0
              };
              if (f.props.$children) {
                const h = f.props.$children;
                m.props.$children = h.map((_) => s(_));
              }
              return m;
            }, o = s(u.brick), a = (f, p, m) => {
              i.brickMap.set(f.id, {
                brick: f,
                sectionId: p,
                parentId: m
              }), f.props.$children && f.props.$children.forEach((_) => a(_, p, f.id));
            }, { sectionId: d, parentId: l } = u;
            if (l) {
              const f = I(l, i);
              if (f != null && f.props.$children) {
                const m = f.props.$children.findIndex((h) => h.id === r);
                f.props.$children.splice(m + 1, 0, o);
              }
            } else {
              const f = i.sections.find((p) => p.id === d);
              if (f) {
                const p = f.bricks.findIndex((m) => m.id === r);
                f.bricks.splice(p + 1, 0, o);
              }
            }
            a(o, d, l);
          }),
          updateBrickProps: (r, i, u) => n((s) => {
            const o = I(r, s);
            o ? (u ? o.mobileProps = j({}, o.mobileProps, i, {
              lastTouched: Date.now()
            }) : o.props = j({}, o.props, i, {
              lastTouched: Date.now()
            }), s.brickMap = A(s.sections)) : console.error("Cannot update brick %s, it does not exist in the brick map", r);
          }),
          updateSectionProps: (r, i, u) => n((s) => {
            const o = s.sections.find((a) => a.id === r);
            o ? u ? o.mobileProps = j({}, o.mobileProps, i, {
              lastTouched: Date.now()
            }) : o.props = j({}, o.props, i, {
              lastTouched: Date.now()
            }) : console.warn("Cannot update section %s, it does not exist", r);
          }),
          /**
           * 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
           */
          moveBrickWithin: (r, i) => n((u) => {
            let s = [], o = -1;
            const a = u.getParentBrick(r);
            a ? (s = I(a.id, u).props.$children, o = s.findIndex((p) => p.id === r)) : (s = ke(r, u).bricks, o = s.findIndex((p) => p.id === r));
            const d = i === "previous" ? Math.max(0, o - 1) : Math.min(s.length - 1, o + 1);
            if (d === o)
              return;
            const [l] = s.splice(o, 1);
            s.splice(d, 0, l), u.brickMap = A(u.sections);
          }),
          /**
           * Reorder a brick within its section using indices
           */
          reorderBrickWithin: (r, i) => n((u) => {
            const s = u.brickMap.get(r);
            if (!s) {
              console.error("Cannot reorder brick %s, brick mapping not found", r);
              return;
            }
            const { sectionId: o, parentId: a } = s;
            if (a) {
              const d = I(a, u);
              if (d != null && d.props.$children) {
                const l = d.props.$children, f = l.findIndex((m) => m.id === r);
                if (f === -1) {
                  console.error("Cannot reorder brick %s, it does not exist in parent's children", r);
                  return;
                }
                const [p] = l.splice(f, 1);
                l.splice(i, 0, p);
              }
            } else {
              const d = u.sections.find((l) => l.id === o);
              if (d) {
                const l = d.bricks.findIndex((p) => p.id === r);
                if (l === -1) {
                  console.error(
                    "Cannot reorder brick %s, it does not exist in section %s",
                    r,
                    o
                  );
                  return;
                }
                const [f] = d.bricks.splice(l, 1);
                d.bricks.splice(i, 0, f);
              }
            }
          }),
          moveBrickToContainerBrick: (r, i, u) => n((s) => {
            const o = s.getBrick(r), a = I(i, s), d = s.brickMap.get(r);
            if (!o || !a || !d) {
              console.error("Cannot move brick %s to new parent, brick or new parent is null", r);
              return;
            }
            if (!("$children" in a.props)) {
              console.error("Cannot move brick %s, parent brick %s is not a container", r, i);
              return;
            }
            const { sectionId: l, parentId: f } = d;
            if (f) {
              const m = I(f, s);
              m != null && m.props.$children && (m.props.$children = m.props.$children.filter(
                (h) => h.id !== r
              ));
            } else {
              const m = s.sections.find((h) => h.id === l);
              m && (m.bricks = m.bricks.filter((h) => h.id !== r));
            }
            a.props.$children || (a.props.$children = []), a.props.$children.splice(u, 0, o);
            const p = s.brickMap.get(i);
            if (p) {
              s.brickMap.set(r, {
                brick: o,
                sectionId: p.sectionId,
                parentId: i
              });
              const m = (h, _) => {
                var S;
                const b = s.brickMap.get(h);
                (S = b == null ? void 0 : b.brick.props) != null && S.$children && b.brick.props.$children.forEach((y) => {
                  s.brickMap.set(y.id, {
                    brick: y,
                    sectionId: _,
                    parentId: h
                  }), m(y.id, _);
                });
              };
              m(r, p.sectionId);
            }
          }),
          moveBrickToSection: (r, i, u) => n((s) => {
            var m;
            const o = s.getBrick(r);
            i || (i = (m = s.brickMap.get(r)) == null ? void 0 : m.sectionId);
            const a = s.sections.find((h) => h.id === i), d = s.brickMap.get(r);
            if (!o || !a || !d) {
              console.error(
                "Cannot move brick %s to section %s, brick or section not found",
                r,
                i
              );
              return;
            }
            const { sectionId: l, parentId: f } = d;
            if (f) {
              const h = I(f, s);
              h != null && h.props.$children && (h.props.$children = h.props.$children.filter(
                (_) => _.id !== r
              ));
            } else {
              const h = s.sections.find((_) => _.id === l);
              h && (h.bricks = h.bricks.filter((_) => _.id !== r));
            }
            typeof u == "number" ? a.bricks.splice(u, 0, o) : a.bricks.push(o), s.brickMap.set(r, {
              brick: o,
              sectionId: i,
              parentId: null
              // Top level in section
            });
            const p = (h, _, b) => {
              var v;
              const S = s.brickMap.get(h);
              (v = S == null ? void 0 : S.brick.props) != null && v.$children && S.brick.props.$children.forEach((x) => {
                s.brickMap.set(x.id, {
                  brick: x,
                  sectionId: _,
                  parentId: h
                }), p(x.id, _);
              });
            };
            p(r, i);
          }),
          /**
           * @deprecated Use getBrickFromDraft instead
           */
          getBrick: (r) => {
            var i;
            return (i = c().brickMap.get(r)) == null ? void 0 : i.brick;
          },
          getParentBrick: (r) => {
            var s;
            const i = c().brickMap, { parentId: u } = i.get(r) ?? {};
            if (u)
              return (s = i.get(u)) == null ? void 0 : s.brick;
          },
          getPositionWithinParent: (r) => {
            const i = c().getParentBrick(r);
            if (i && "$children" in i.props)
              return i.props.$children.findIndex((u) => u.id === r);
          },
          canMoveToWithinParent: (r, i) => {
            const u = c().getParentBrick(r);
            if (u && "$children" in u.props) {
              const a = u.props.$children, d = a.findIndex((l) => l.id === r);
              return i === "previous" ? d > 0 : d < a.length - 1;
            }
            const s = ke(r, c()), o = s.bricks.findIndex((a) => a.id === r);
            return i === "previous" ? o > 0 : o < (s == null ? void 0 : s.bricks.length) - 1;
          },
          setPreviewTheme: (r) => n((i) => {
            i.previewTheme = r;
          }),
          validatePreviewTheme: (r) => n((i) => {
            i.previewTheme && r && (i.theme = i.previewTheme), i.previewTheme = void 0;
          }),
          cancelPreviewTheme: () => n((r) => {
            r.previewTheme = void 0;
          }),
          setTheme: (r) => n((i) => {
            i.theme = r;
          }),
          toggleBrickVisibility: (r, i) => n((u) => {
            var o, a;
            const s = I(r, u);
            s && ((o = s.props).hidden ?? (o.hidden = { desktop: !1, mobile: !1 }), s.props.hidden[i] = !((a = s.props.hidden) != null && a[i]));
          }),
          addBrick: (r, i, u, s) => n((o) => {
            var d;
            const a = o.sections.find((l) => l.id === i);
            if (!a) {
              console.error("Cannot add brick, section %s does not exist", i);
              return;
            }
            if (s) {
              if (console.log("Adding brick to parent container", s), !Ct(s, o)) {
                console.warn("Cannot add brick, parent container %s does not exist", s);
                return;
              }
              const f = o.sections.find((m) => m.id === i);
              if (!f) {
                console.warn("Cannot add brick, parent section %s does not exist", i);
                return;
              }
              const p = f.bricks.find((m) => m.id === s);
              if (!p) {
                console.warn(
                  "Cannot add brick, parent brick %s does not exist in section %s",
                  s,
                  i
                );
                return;
              }
              p.props.$children || (console.warn("Brick added to a container without $children, initializing it"), p.props.$children = []), p.props.$children.push(r);
            } else
              a.bricks.splice(u, 0, r);
            if (o.brickMap.set(r.id, {
              brick: r,
              sectionId: i,
              parentId: s
            }), (d = r.props) != null && d.$children) {
              const l = (f, p) => {
                f.forEach((m) => {
                  var h;
                  o.brickMap.set(m.id, {
                    brick: m,
                    sectionId: i,
                    parentId: p
                  }), (h = m.props) != null && h.$children && l(m.props.$children, m.id);
                });
              };
              console.log("Adding children to brick map", r.props.$children), l(r.props.$children, r.id);
            }
          }),
          updateAttributes: (r) => n((i) => {
            i.attr = { ...c().attr, ...r };
          }),
          setVersion: (r) => n((i) => {
            i.version = r;
          }),
          adjustMobileLayout: () => n((r) => {
          }),
          setLastSaved: (r) => n((i) => {
            i.lastSaved = r;
          }),
          setDirty: (r) => n((i) => {
            i.dirty = r;
          })
        })),
        {
          // limit undo history to 100
          limit: 100,
          equality: (n, c) => Re(n, c),
          partialize: (n) => Object.fromEntries(
            Object.entries(n).filter(([c]) => ["sections", "attr"].includes(c))
          )
          // handleSet: (handleSet) =>
          //   debounce<typeof handleSet>((state) => {
          //     if (state) {
          //       handleSet(state);
          //     }
          //   }, 200),
        }
      )
    )
  );
}, Bt = Ye(null), E = () => {
  const e = Xe(Bt);
  return L(e, "useDraftStoreContext must be used within a DraftStoreContext"), e;
}, Nt = () => E().temporal.getState(), Wt = () => {
  const e = E();
  return w(e, (t) => t.sitemap);
}, jt = () => {
  const e = E();
  return w(e, (t) => {
    const n = t.sitemap.length > 0, c = t.themes.length > 0, r = n && c && t.sitemap.length > 0, i = new URL(window.location.href).searchParams.get("action") === "generate";
    return {
      hasSitemap: n,
      hasThemesGenerated: c,
      sitemap: t.sitemap,
      pages: t.pages,
      isReady: !i || r || !1
    };
  });
}, Ht = () => {
  const e = E(), t = w(e, (c) => c.pages);
  return {
    site: It(),
    pages: t
  };
}, Ut = () => {
  const e = E();
  return w(e, (t) => t.themes);
}, Vt = () => {
  const e = E();
  return w(e);
};
function Gt(e) {
  const t = E(), n = w(t, (r) => r.getParentBrick);
  let c = n(e);
  for (; c; ) {
    if (c.type === "dynamic")
      return !0;
    e = c.id, c = n(e);
  }
  return !1;
}
function Kt(e) {
  const t = E(), n = w(t, (r) => r.getParentBrick);
  let c = n(e);
  for (; c; ) {
    if (c.type === "dynamic")
      return c;
    e = c.id, c = n(e);
  }
  return null;
}
function qt(e) {
  const t = E();
  return w(t, (c) => c.getParentBrick)(e);
}
function Jt() {
  const e = E();
  return w(e, (t) => t.version);
}
function Yt() {
  const e = E();
  return w(e, (t) => t.lastSaved);
}
const Xt = () => {
  const e = E(), t = w(e, (n) => n.sections);
  return qe(t);
}, Qt = (e) => {
  const t = E();
  return w(t, (n) => {
    const c = n.sections.find((r) => e && r.id === e);
    return c ? {
      section: c,
      bricks: Array.from(n.brickMap).filter(([, r]) => r.sectionId === e)
    } : null;
  });
}, Zt = (e) => {
  const t = E();
  return w(t, (n) => e ? I(e, n) : null);
}, er = (e) => {
  const t = E();
  return w(t, (n) => ke(e, n));
}, tr = () => {
  const e = E();
  return w(e, (t) => t.attr);
}, rr = () => {
  const e = E();
  return w(e, (t) => t.attributes ?? t.siteAttributes);
}, nr = () => {
  const e = E();
  return w(e, (t) => ({
    deleteBrick: t.deleteBrick,
    detachBrickFromContainer: t.detachBrickFromContainer,
    setSections: t.setSections,
    setThemes: t.setThemes,
    setTheme: t.setTheme,
    pickTheme: t.pickTheme,
    setSitemap: t.setSitemap,
    addPage: t.addPage,
    addDatasource: t.addDatasource,
    addDatarecord: t.addDatarecord,
    duplicateBrick: t.duplicateBrick,
    addBrick: t.addBrick,
    getBrick: t.getBrick,
    validatePreviewTheme: t.validatePreviewTheme,
    adjustMobileLayout: t.adjustMobileLayout,
    duplicateSection: t.duplicateSection,
    toggleBrickVisibilityPerBreakpoint: t.toggleBrickVisibility,
    getParentBrick: t.getParentBrick,
    updateBrickProps: t.updateBrickProps,
    moveBrickWithin: t.moveBrickWithin,
    reorderBrickWithin: t.reorderBrickWithin,
    getPositionWithinParent: t.getPositionWithinParent,
    canMoveToWithinParent: t.canMoveToWithinParent,
    moveBrickToContainerBrick: t.moveBrickToContainerBrick,
    moveBrickToSection: t.moveBrickToSection,
    deleteSection: t.deleteSection,
    moveSectionUp: t.moveSectionUp,
    moveSectionDown: t.moveSectionDown,
    reorderSections: t.reorderSections,
    updateSection: t.updateSection,
    updateSectionProps: t.updateSectionProps,
    addSection: t.addSection,
    createEmptySection: t.createEmptySection,
    isFirstSection: t.isFirstSection,
    isLastSection: t.isLastSection
  }));
}, ir = () => {
  const e = E();
  return w(e, (t) => ({
    id: t.id,
    path: t.path,
    label: t.label,
    siteLabel: t.siteLabel,
    siteId: t.siteId,
    hostname: t.hostname
  }));
}, It = () => {
  const e = E();
  return w(e, (n) => ({
    id: n.siteId,
    label: n.label,
    sitemap: n.sitemap,
    attr: n.attr,
    theme: n.theme,
    themes: n.themes,
    attributes: n.siteAttributes,
    hostname: n.hostname,
    datasources: n.datasources,
    datarecords: n.datarecords,
    sitePrompt: n.sitePrompt
  }));
}, or = () => {
  const e = E();
  return w(e, (t) => t.theme);
}, cr = (e) => {
  const t = E();
  ee(() => t.subscribe((n) => (n == null ? void 0 : n.sections) ?? [], Je(e, 200), {
    fireImmediately: !1
  }), []);
}, sr = (e) => {
  const t = E();
  ee(() => t.subscribe((n) => n.attr, e, {
    equalityFn: Re
  }), []);
}, ar = (e) => {
  const t = E();
  ee(() => t.subscribe((n) => n.theme, e), []);
}, ur = (e) => {
  const t = E();
  ee(() => t.subscribe((n) => n.path, e), []);
};
function Ct(e, t) {
  const { brick: n } = t.brickMap.get(e) ?? {};
  return n;
}
function ke(e, t) {
  const n = t.brickMap.get(e);
  if (n)
    return t.sections.find((c) => c.id === n.sectionId);
}
function I(e, t) {
  for (const n of t.sections) {
    const c = n.bricks.find((u) => u.id === e);
    if (c)
      return c;
    const r = (u) => {
      var s;
      for (const o of u) {
        if (o.id === e)
          return o;
        if ((s = o.props) != null && s.$children) {
          const a = r(o.props.$children);
          if (a)
            return a;
        }
      }
    }, i = r(n.bricks);
    if (i)
      return i;
  }
  return null;
}
const dr = () => {
  const e = E();
  return w(e, (t) => t.sitePrompt);
};
function A(e) {
  const t = /* @__PURE__ */ new Map();
  function n(c, r, i) {
    var u;
    t.set(c.id, { brick: c, sectionId: r, parentId: i }), (u = c.props) != null && u.$children && c.props.$children.forEach((o) => n(o, r, c.id));
  }
  return e.forEach((c) => {
    c.bricks.forEach((r) => n(r, c.id, null));
  }), t;
}
export {
  ut as A,
  w as B,
  E as C,
  Bt as D,
  Ue as E,
  xt as F,
  Rt as G,
  gt as H,
  Mt as I,
  Gt as J,
  It as K,
  ir as a,
  cr as b,
  sr as c,
  ur as d,
  ar as e,
  nr as f,
  Zt as g,
  Nt as h,
  jt as i,
  or as j,
  Xt as k,
  Ut as l,
  Ft as m,
  Jt as n,
  Yt as o,
  Wt as p,
  dr as q,
  Ht as r,
  Qt as s,
  L as t,
  Vt as u,
  tr as v,
  Kt as w,
  er as x,
  qt as y,
  rr as z
};
//# sourceMappingURL=use-page-data.BTws06Go.js.map
