function serializeForm(form) {
  return {
    id: form.id,
    name: form.name,
    slug: form.slug,
    description: form.description,
    schema: form.schema,
    successMessage: form.successMessage,
    redirectUrl: form.redirectUrl,
    status: form.status,
    createdBy: form.createdBy,
    createdAt: form.createdAt.toISOString(),
    updatedAt: form.updatedAt.toISOString()
  };
}
function serializeFormSubmission(submission) {
  return {
    ...submission,
    submittedAt: submission.submittedAt.toISOString()
  };
}
function serializeFormSubmissionWithData(submission) {
  let parsedData = null;
  try {
    parsedData = JSON.parse(submission.data);
  } catch {
  }
  return {
    ...serializeFormSubmission(submission),
    parsedData,
    form: submission.form ? serializeForm(submission.form) : void 0
  };
}
async function getAllForms(adapter, params) {
  const whereConditions = [];
  if (params?.status) {
    whereConditions.push({
      field: "status",
      value: params.status,
      operator: "eq"
    });
  }
  const total = await adapter.count({
    model: "form",
    where: whereConditions.length > 0 ? whereConditions : void 0
  });
  const forms = await adapter.findMany({
    model: "form",
    where: whereConditions.length > 0 ? whereConditions : void 0,
    limit: params?.limit,
    offset: params?.offset,
    sortBy: { field: "createdAt", direction: "desc" }
  });
  return {
    items: forms.map(serializeForm),
    total,
    limit: params?.limit,
    offset: params?.offset
  };
}
async function getFormById(adapter, id) {
  const form = await adapter.findOne({
    model: "form",
    where: [{ field: "id", value: id, operator: "eq" }]
  });
  if (!form) return null;
  return serializeForm(form);
}
async function getFormBySlug(adapter, slug) {
  const form = await adapter.findOne({
    model: "form",
    where: [{ field: "slug", value: slug, operator: "eq" }]
  });
  if (!form) {
    return null;
  }
  return serializeForm(form);
}
async function getFormSubmissions(adapter, formId, params) {
  const form = await adapter.findOne({
    model: "form",
    where: [{ field: "id", value: formId, operator: "eq" }]
  });
  if (!form) {
    return {
      items: [],
      total: 0,
      limit: params?.limit,
      offset: params?.offset
    };
  }
  const total = await adapter.count({
    model: "formSubmission",
    where: [{ field: "formId", value: formId, operator: "eq" }]
  });
  const submissions = await adapter.findMany({
    model: "formSubmission",
    where: [{ field: "formId", value: formId, operator: "eq" }],
    limit: params?.limit,
    offset: params?.offset,
    sortBy: { field: "submittedAt", direction: "desc" },
    join: { form: true }
  });
  return {
    items: submissions.map(serializeFormSubmissionWithData),
    total,
    limit: params?.limit,
    offset: params?.offset
  };
}

export { getAllForms, getFormById, getFormBySlug, getFormSubmissions, serializeForm, serializeFormSubmission, serializeFormSubmissionWithData };
