import { defineRegistry } from "../../../share/define-registry.js";
const schemaModules = import.meta.glob(
  "../fields/*/*/schema.{ts,js}",
  { eager: true, exhaustive: true }
);
const configModules = import.meta.glob(
  "../fields/*/*/config.vue",
  { eager: true, import: "default", exhaustive: true }
);
const runtimeModules = import.meta.glob(
  "../fields/*/*/runtime.vue",
  { eager: true, import: "default", exhaustive: true }
);
const sourceKey = (type, compatibilityDate) => `${type}@${compatibilityDate}`;
const registry = defineRegistry({
  hostName: "shwfed-form",
  dirSegment: "fields",
  schemaModules,
  configModules,
  runtimeModules,
  // An entry is deprecated by one of two rules:
  //   (a) another entry shares its `type` with a strictly greater
  //       `compatibilityDate` — date-versioned supersession;
  //   (b) another entry declares this one in its `migrateFrom` — explicit
  //       cross-type supersession (rename, split, merge).
  // Rule (a) wins on overlap, since the date-versioned rule is the implicit
  // convention; an explicit `migrateFrom` against a still-current entry is
  // almost always a mistake, but we let it slide as long as no two targets
  // claim the same source (that collision throws below).
  deriveEntryExtras: (drafts) => {
    const latestByType = /* @__PURE__ */ new Map();
    for (const d of drafts) {
      const prev = latestByType.get(d.type);
      if (!prev || d.compatibilityDate > prev) latestByType.set(d.type, d.compatibilityDate);
    }
    const migrationTargets = /* @__PURE__ */ new Map();
    for (const d of drafts) {
      const sources = d.module.migrateFrom;
      if (!sources || sources.length === 0) continue;
      for (const src of sources) {
        const key = sourceKey(src.type, src.compatibilityDate);
        const existing = migrationTargets.get(key);
        if (existing) {
          throw new Error(
            `[shwfed-form] migrateFrom collision: source "${key}" is claimed by both "${sourceKey(existing.type, existing.compatibilityDate)}" and "${sourceKey(d.type, d.compatibilityDate)}"`
          );
        }
        migrationTargets.set(key, { type: d.type, compatibilityDate: d.compatibilityDate });
      }
    }
    return (draft) => {
      const declared = migrationTargets.get(sourceKey(draft.type, draft.compatibilityDate));
      if (declared) {
        return { deprecated: true, supersededBy: declared };
      }
      const latest = latestByType.get(draft.type);
      if (draft.compatibilityDate < latest) {
        return {
          deprecated: true,
          supersededBy: { type: draft.type, compatibilityDate: latest }
        };
      }
      return { deprecated: false };
    };
  }
});
let _migrationTargets = null;
function getMigrationTargets() {
  if (_migrationTargets) return _migrationTargets;
  const map = /* @__PURE__ */ new Map();
  for (const entry of registry.ENTRIES) {
    const sources = entry.module.migrateFrom;
    if (!sources || sources.length === 0) continue;
    for (const src of sources) {
      map.set(sourceKey(src.type, src.compatibilityDate), {
        type: entry.type,
        compatibilityDate: entry.compatibilityDate
      });
    }
  }
  _migrationTargets = map;
  return map;
}
let _fields = null;
function getFields() {
  if (_fields) return _fields;
  _fields = registry.ENTRIES.map((e) => ({
    type: e.type,
    compatibilityDate: e.compatibilityDate,
    metadata: e.module.metadata,
    schema: e.module.schema,
    config: e.config,
    runtime: e.runtime,
    deprecated: e.deprecated,
    ...e.supersededBy !== void 0 ? { supersededBy: e.supersededBy } : {},
    migrate: e.module.migrate,
    ...e.module.migrateFrom !== void 0 ? { migrateFrom: e.module.migrateFrom } : {},
    defaults: e.module.defaults
  }));
  return _fields;
}
export const FIELDS = new Proxy([], {
  get(_, prop) {
    const list = getFields();
    const v = list[prop];
    return typeof v === "function" ? v.bind(list) : v;
  },
  has(_, prop) {
    return prop in getFields();
  },
  ownKeys() {
    return Reflect.ownKeys(getFields());
  },
  getOwnPropertyDescriptor(_, prop) {
    return Reflect.getOwnPropertyDescriptor(getFields(), prop);
  }
});
export function allFieldSchemas(configure) {
  return getFields().map((entry) => entry.schema(configure).annotations({
    identifier: `${entry.type}@${entry.compatibilityDate}`
  }));
}
export function findField(type, compatibilityDate) {
  return getFields().find((f) => f.type === type && f.compatibilityDate === compatibilityDate);
}
export function findMigrationTarget(type, compatibilityDate) {
  const target = getMigrationTargets().get(sourceKey(type, compatibilityDate));
  if (!target) return void 0;
  return findField(target.type, target.compatibilityDate);
}
