import { defineRegistry } from "../../../share/define-registry.js";
const schemaModules = import.meta.glob(
  "../columns/*/*/schema.{ts,js}",
  { eager: true, exhaustive: true }
);
const configModules = import.meta.glob(
  "../columns/*/*/config.vue",
  { eager: true, import: "default", exhaustive: true }
);
const runtimeModules = import.meta.glob(
  "../columns/*/*/runtime.vue",
  { eager: true, import: "default", exhaustive: true }
);
const sourceKey = (type, compatibilityDate) => `${type}@${compatibilityDate}`;
const registry = defineRegistry({
  hostName: "shwfed-table",
  dirSegment: "columns",
  schemaModules,
  configModules,
  runtimeModules,
  validateModule: (mod, type, date) => {
    if (typeof mod.toColumnDef !== "function") {
      throw new TypeError(`[shwfed-table] column "${type}@${date}" is missing toColumnDef export`);
    }
  },
  // Deprecation rules mirror the form host:
  //   (a) date-versioned: another entry shares this entry's `type` with a
  //       strictly greater `compatibilityDate`;
  //   (b) explicit cross-type: another entry declares this one in its
  //       `migrateFrom`. Same source claimed twice → throw.
  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 migrationTargets2 = /* @__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 = migrationTargets2.get(key);
        if (existing) {
          throw new Error(
            `[shwfed-table] migrateFrom collision: source "${key}" is claimed by both "${sourceKey(existing.type, existing.compatibilityDate)}" and "${sourceKey(d.type, d.compatibilityDate)}"`
          );
        }
        migrationTargets2.set(key, { type: d.type, compatibilityDate: d.compatibilityDate });
      }
    }
    return (draft) => {
      const declared = migrationTargets2.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 };
    };
  }
});
export const COLUMNS = registry.ENTRIES.map((e) => ({
  type: e.type,
  compatibilityDate: e.compatibilityDate,
  metadata: e.module.metadata,
  schema: e.module.schema,
  config: e.config,
  runtime: e.runtime,
  toColumnDef: e.module.toColumnDef,
  deprecated: e.deprecated,
  ...e.supersededBy !== void 0 ? { supersededBy: e.supersededBy } : {},
  ...e.module.migrate !== void 0 ? { migrate: e.module.migrate } : {},
  ...e.module.migrateFrom !== void 0 ? { migrateFrom: e.module.migrateFrom } : {},
  ...e.module.defaults !== void 0 ? { defaults: e.module.defaults } : {}
}));
const migrationTargets = (() => {
  const map = /* @__PURE__ */ new Map();
  for (const entry of COLUMNS) {
    const sources = entry.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
      });
    }
  }
  return map;
})();
export function allColumnSchemas(configure) {
  return COLUMNS.map((entry) => entry.schema(configure).annotations({
    identifier: `${entry.type}@${entry.compatibilityDate}`
  }));
}
export function findColumn(type, compatibilityDate) {
  return COLUMNS.find((r) => r.type === type && r.compatibilityDate === compatibilityDate);
}
export function findMigrationTarget(type, compatibilityDate) {
  const target = migrationTargets.get(sourceKey(type, compatibilityDate));
  if (!target) return void 0;
  return findColumn(target.type, target.compatibilityDate);
}
export function latestColumnByType() {
  const byType = /* @__PURE__ */ new Map();
  for (const r of COLUMNS) {
    const prev = byType.get(r.type);
    if (!prev || r.compatibilityDate > prev.compatibilityDate) {
      byType.set(r.type, r);
    }
  }
  return [...byType.values()];
}
