function serializeContentType(ct) {
  const needsMigration = !ct.autoFormVersion || ct.autoFormVersion < 2;
  const migratedJsonSchema = needsMigration ? migrateToUnifiedSchema(ct.jsonSchema, ct.fieldConfig) : ct.jsonSchema;
  return {
    id: ct.id,
    name: ct.name,
    slug: ct.slug,
    description: ct.description,
    jsonSchema: migratedJsonSchema,
    createdAt: ct.createdAt.toISOString(),
    updatedAt: ct.updatedAt.toISOString()
  };
}
function migrateToUnifiedSchema(jsonSchemaStr, fieldConfigStr) {
  if (!fieldConfigStr) return jsonSchemaStr;
  try {
    const jsonSchema = JSON.parse(jsonSchemaStr);
    const fieldConfig = JSON.parse(fieldConfigStr);
    if (!jsonSchema.properties || typeof fieldConfig !== "object") {
      return jsonSchemaStr;
    }
    for (const [key, config] of Object.entries(fieldConfig)) {
      if (jsonSchema.properties[key] && typeof config === "object" && config !== null && "fieldType" in config) {
        jsonSchema.properties[key].fieldType = config.fieldType;
      }
    }
    return JSON.stringify(jsonSchema);
  } catch {
    return jsonSchemaStr;
  }
}
function serializeContentItem(item) {
  return {
    ...item,
    createdAt: item.createdAt.toISOString(),
    updatedAt: item.updatedAt.toISOString()
  };
}
function serializeContentItemWithType(item) {
  const parsedData = JSON.parse(item.data);
  return {
    ...serializeContentItem(item),
    parsedData,
    contentType: item.contentType ? serializeContentType(item.contentType) : void 0
  };
}
async function getAllContentTypes(adapter) {
  const contentTypes = await adapter.findMany({
    model: "contentType",
    sortBy: { field: "name", direction: "asc" }
  });
  return contentTypes.map(serializeContentType);
}
async function getAllContentItems(adapter, contentTypeSlug, params) {
  const contentType = await adapter.findOne({
    model: "contentType",
    where: [
      {
        field: "slug",
        value: contentTypeSlug,
        operator: "eq"
      }
    ]
  });
  if (!contentType) {
    return {
      items: [],
      total: 0,
      limit: params?.limit,
      offset: params?.offset
    };
  }
  const whereConditions = [
    {
      field: "contentTypeId",
      value: contentType.id,
      operator: "eq"
    }
  ];
  if (params?.slug) {
    whereConditions.push({
      field: "slug",
      value: params.slug,
      operator: "eq"
    });
  }
  const total = await adapter.count({
    model: "contentItem",
    where: whereConditions
  });
  const items = await adapter.findMany({
    model: "contentItem",
    where: whereConditions,
    limit: params?.limit,
    offset: params?.offset,
    sortBy: { field: "createdAt", direction: "desc" },
    join: { contentType: true }
  });
  return {
    items: items.map(serializeContentItemWithType),
    total,
    limit: params?.limit,
    offset: params?.offset
  };
}
async function getContentItemById(adapter, id) {
  const item = await adapter.findOne({
    model: "contentItem",
    where: [{ field: "id", value: id, operator: "eq" }],
    join: { contentType: true }
  });
  if (!item) return null;
  return serializeContentItemWithType(item);
}
async function getContentItemBySlug(adapter, contentTypeSlug, slug) {
  const contentType = await adapter.findOne({
    model: "contentType",
    where: [
      {
        field: "slug",
        value: contentTypeSlug,
        operator: "eq"
      }
    ]
  });
  if (!contentType) {
    return null;
  }
  const item = await adapter.findOne({
    model: "contentItem",
    where: [
      {
        field: "contentTypeId",
        value: contentType.id,
        operator: "eq"
      },
      { field: "slug", value: slug, operator: "eq" }
    ],
    join: { contentType: true }
  });
  if (!item) {
    return null;
  }
  return serializeContentItemWithType(item);
}

export { getAllContentItems, getAllContentTypes, getContentItemById, getContentItemBySlug, migrateToUnifiedSchema, serializeContentItem, serializeContentItemWithType, serializeContentType };
