// @bun
var __create = Object.create;
var __getProtoOf = Object.getPrototypeOf;
var __defProp = Object.defineProperty;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __hasOwnProp = Object.prototype.hasOwnProperty;
function __accessProp(key) {
  return this[key];
}
var __toESMCache_node;
var __toESMCache_esm;
var __toESM = (mod, isNodeMode, target) => {
  var canCache = mod != null && typeof mod === "object";
  if (canCache) {
    var cache = isNodeMode ? __toESMCache_node ??= new WeakMap : __toESMCache_esm ??= new WeakMap;
    var cached = cache.get(mod);
    if (cached)
      return cached;
  }
  target = mod != null ? __create(__getProtoOf(mod)) : {};
  const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
  for (let key of __getOwnPropNames(mod))
    if (!__hasOwnProp.call(to, key))
      __defProp(to, key, {
        get: __accessProp.bind(mod, key),
        enumerable: true
      });
  if (canCache)
    cache.set(mod, to);
  return to;
};
var __toCommonJS = (from) => {
  var entry = (__moduleCache ??= new WeakMap).get(from), desc;
  if (entry)
    return entry;
  entry = __defProp({}, "__esModule", { value: true });
  if (from && typeof from === "object" || typeof from === "function") {
    for (var key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(entry, key))
        __defProp(entry, key, {
          get: __accessProp.bind(from, key),
          enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
        });
  }
  __moduleCache.set(from, entry);
  return entry;
};
var __moduleCache;
var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
var __returnValue = (v) => v;
function __exportSetter(name, newValue) {
  this[name] = __returnValue.bind(null, newValue);
}
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, {
      get: all[name],
      enumerable: true,
      configurable: true,
      set: __exportSetter.bind(all, name)
    });
};
var __legacyDecorateClassTS = function(decorators, target, key, desc) {
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
    r = Reflect.decorate(decorators, target, key, desc);
  else
    for (var i = decorators.length - 1;i >= 0; i--)
      if (d = decorators[i])
        r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __legacyMetadataTS = (k, v) => {
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function")
    return Reflect.metadata(k, v);
};
var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
var __require = import.meta.require;

// src/core/islandManifest.ts
var toIslandFrameworkSegment = (framework) => framework[0]?.toUpperCase() + framework.slice(1), collectFrameworkIslands = (manifest, prefix) => {
  const entries = {};
  let found = false;
  for (const [key, value] of Object.entries(manifest)) {
    if (!key.startsWith(prefix))
      continue;
    const component = key.slice(prefix.length);
    if (!component)
      continue;
    entries[component] = value;
    found = true;
  }
  return found ? entries : undefined;
}, getIslandManifestEntries = (manifest) => {
  const islands = {};
  const frameworks = ["react", "svelte", "vue", "angular"];
  for (const framework of frameworks) {
    const prefix = `Island${toIslandFrameworkSegment(framework)}`;
    const entries = collectFrameworkIslands(manifest, prefix);
    if (entries)
      islands[framework] = entries;
  }
  return islands;
}, getIslandManifestKey = (framework, component) => `Island${toIslandFrameworkSegment(framework)}${component}`;

// src/core/islands.ts
function getIslandComponent(component) {
  if (isIslandComponentDefinition(component)) {
    return component.component;
  }
  return component;
}
var defineIslandComponent = (component, options) => ({
  component,
  export: options.export,
  source: options.source
}), defineIslandRegistry = (registry) => registry, isRecord = (value) => typeof value === "object" && value !== null, getIslandBuildReference = (component) => {
  if (!isIslandComponentDefinition(component))
    return null;
  return {
    export: component.export,
    source: component.source
  };
}, isIslandComponentDefinition = (value) => isRecord(value) && ("component" in value) && ("source" in value) && typeof value.source === "string", parseIslandProps = (rawProps) => {
  if (!rawProps)
    return {};
  return JSON.parse(rawProps);
}, serializeIslandProps = (props) => JSON.stringify(props ?? {});
var init_islands = () => {};

// src/islands/browser.ts
init_islands();

// node_modules/zustand/esm/vanilla.mjs
var createStoreImpl = (createState) => {
  let state;
  const listeners = /* @__PURE__ */ new Set;
  const setState = (partial, replace) => {
    const nextState = typeof partial === "function" ? partial(state) : partial;
    if (!Object.is(nextState, state)) {
      const previousState = state;
      state = (replace != null ? replace : typeof nextState !== "object" || nextState === null) ? nextState : Object.assign({}, state, nextState);
      listeners.forEach((listener) => listener(state, previousState));
    }
  };
  const getState = () => state;
  const getInitialState = () => initialState;
  const subscribe = (listener) => {
    listeners.add(listener);
    return () => listeners.delete(listener);
  };
  const api = { setState, getState, getInitialState, subscribe };
  const initialState = state = createState(setState, getState, api);
  return api;
};
var createStore = (createState) => createState ? createStoreImpl(createState) : createStoreImpl;

// node_modules/zustand/esm/middleware.mjs
function combine(initialState, create) {
  return (...args) => Object.assign({}, initialState, create(...args));
}

// src/client/islandStore.ts
var getIslandStoreSnapshot = () => {
  globalThis.__ABS_ISLAND_STATE__ ??= {};
  return globalThis.__ABS_ISLAND_STATE__;
};
var getIslandStores = () => {
  globalThis.__ABS_ISLAND_STORES__ ??= new Map;
  return globalThis.__ABS_ISLAND_STORES__;
};
var isSerializableValue = (value) => typeof value !== "function" && value !== undefined;
var toSerializableState = (state) => Object.fromEntries(Object.entries(state).filter(([, value]) => isSerializableValue(value)));
var applySnapshot = (store, snapshot) => {
  if (!snapshot) {
    return;
  }
  store.setState({
    ...store.getState(),
    ...snapshot
  });
};
var getPeerStores = (storeInstances, ownerStore) => [...storeInstances].filter((peer) => peer.store !== ownerStore);
var syncIslandSnapshot = (storeId, state, storeInstances, ownerStore) => {
  const nextSnapshot = toSerializableState(state);
  getIslandStoreSnapshot()[storeId] = nextSnapshot;
  for (const peerStore of getPeerStores(storeInstances, ownerStore)) {
    peerStore.applyExternalSnapshot(nextSnapshot);
  }
};
var createIslandStore = (storeId, initialState, createState) => {
  const store = createStore(combine(initialState, createState));
  const stores = getIslandStores();
  const storeInstances = stores.get(storeId) ?? new Set;
  const initialSnapshot = getIslandStoreSnapshot()[storeId];
  applySnapshot(store, initialSnapshot);
  let isApplyingExternalSnapshot = false;
  const applyExternalSnapshot = (snapshot) => {
    isApplyingExternalSnapshot = true;
    applySnapshot(store, snapshot);
  };
  storeInstances.add({
    applyExternalSnapshot,
    store
  });
  stores.set(storeId, storeInstances);
  syncIslandSnapshot(storeId, store.getState(), storeInstances, store);
  store.subscribe((state) => {
    if (isApplyingExternalSnapshot) {
      isApplyingExternalSnapshot = false;
      return;
    }
    syncIslandSnapshot(storeId, state, storeInstances, store);
  });
  return store;
};
var getIslandStoreServerSnapshot = (store, selector) => selector(store.getInitialState());
var applySnapshotToStoreInstances = (storeId, instances, snapshot) => {
  for (const instance of instances) {
    instance.applyExternalSnapshot(snapshot[storeId] ?? {});
  }
};
var initializeIslandStores = (state) => {
  const currentSnapshot = getIslandStoreSnapshot();
  const nextSnapshot = {
    ...state,
    ...currentSnapshot
  };
  globalThis.__ABS_ISLAND_STATE__ = nextSnapshot;
  for (const [storeId, store] of getIslandStores()) {
    applySnapshotToStoreInstances(storeId, store, nextSnapshot);
  }
};
var readIslandStore = (store, selector) => selector(store.getState());
var subscribeIslandStore = (store, selector, listener) => {
  let currentSelection = selector(store.getState());
  return store.subscribe((state) => {
    const nextSelection = selector(state);
    if (Object.is(nextSelection, currentSelection)) {
      return;
    }
    currentSelection = nextSelection;
    listener(nextSelection);
  });
};

// src/islands/browser.ts
var renderIslandMarkup = async () => {
  throw new Error('renderIslandMarkup is server-only. Import from "@absolutejs/absolute/islands" on the server.');
};
export {
  renderIslandMarkup,
  defineIslandRegistry,
  createIslandStore
};

//# debugId=7EC0D5387C81EDED64756E2164756E21
//# sourceMappingURL=browser.js.map
