'use strict';

const getters = require('./getters.cjs');

async function createCMSContentItem(adapter, contentTypeSlug, input) {
  const contentType = await adapter.findOne({
    model: "contentType",
    where: [
      {
        field: "slug",
        value: contentTypeSlug,
        operator: "eq"
      }
    ]
  });
  if (!contentType) {
    throw new Error(`Content type "${contentTypeSlug}" not found`);
  }
  const existing = await adapter.findOne({
    model: "contentItem",
    where: [
      {
        field: "contentTypeId",
        value: contentType.id,
        operator: "eq"
      },
      { field: "slug", value: input.slug, operator: "eq" }
    ]
  });
  if (existing) {
    throw new Error(
      `Content item with slug "${input.slug}" already exists in type "${contentTypeSlug}"`
    );
  }
  const item = await adapter.create({
    model: "contentItem",
    data: {
      contentTypeId: contentType.id,
      slug: input.slug,
      data: JSON.stringify(input.data),
      createdAt: /* @__PURE__ */ new Date(),
      updatedAt: /* @__PURE__ */ new Date()
    }
  });
  return getters.serializeContentItem(item);
}

exports.createCMSContentItem = createCMSContentItem;
