import { Schema } from "effect";
import { defineRegistry } from "../../../share/define-registry.js";
import { registerBlockRef, registerFindBlock } from "./block-ref-cache.js";
const schemaModules = import.meta.glob(
  "../blocks/*/*/schema.{ts,js}",
  { eager: true, exhaustive: true }
);
const configModules = import.meta.glob(
  "../blocks/*/*/config.vue",
  { eager: true, import: "default", exhaustive: true }
);
const runtimeModules = import.meta.glob(
  "../blocks/*/*/runtime.vue",
  { eager: true, import: "default", exhaustive: true }
);
const registry = defineRegistry({
  hostName: "shwfed-config",
  dirSegment: "blocks",
  schemaModules,
  configModules,
  runtimeModules
});
export const BLOCKS = registry.ENTRIES.map((e) => ({
  type: e.type,
  compatibilityDate: e.compatibilityDate,
  metadata: e.module.metadata,
  schema: e.module.schema,
  defaultBody: e.module.defaultBody,
  config: e.config,
  runtime: e.runtime
}));
const unionCache = /* @__PURE__ */ new WeakMap();
function getOrBuildUnion(configure) {
  const cached = unionCache.get(configure);
  if (cached) return cached;
  const entry = {
    blockRef: null,
    members: null,
    union: null
  };
  entry.blockRef = Schema.suspend(() => {
    if (!entry.union) {
      throw new Error("[shwfed-config] BlockRef resolved before union built \u2014 circular factory init");
    }
    return entry.union;
  }).annotations({ identifier: "PageBlock" });
  unionCache.set(configure, entry);
  registerBlockRef(configure, entry.blockRef);
  entry.members = BLOCKS.map((b) => b.schema(configure, entry.blockRef).annotations({
    identifier: `${b.type}@${b.compatibilityDate}`
  }));
  entry.union = entry.members.length === 1 ? entry.members[0] : Schema.Union(...entry.members);
  return entry;
}
export function blockRef(configure) {
  return getOrBuildUnion(configure).blockRef;
}
export function allBlockSchemas(configure) {
  return getOrBuildUnion(configure).members;
}
export function findBlock(type, compatibilityDate) {
  return BLOCKS.find((b) => b.type === type && b.compatibilityDate === compatibilityDate);
}
registerFindBlock(findBlock);
