import { getFormVersion } from "../../helpers.js";
import { categoriseData } from "../machine/v2.js";
import { FormAdapterSubmissionSchemaVersion } from "../../types/enums.js";
export function format(context, items, model, submitResponse, formStatus, formMetadata) {
  const csvFiles = extractCsvFiles(submitResponse);
  const {
    main: v2Main,
    ...v2Data
  } = categoriseData(items);
  const versionMetadata = getFormVersion(model.def);
  const meta = {
    schemaVersion: FormAdapterSubmissionSchemaVersion.V1,
    timestamp: new Date(),
    referenceNumber: context.referenceNumber,
    formName: model.name,
    formId: formMetadata?.id ?? '',
    formSlug: formMetadata?.slug ?? '',
    status: formStatus.state,
    isPreview: formStatus.isPreview,
    notificationEmail: formMetadata?.notificationEmail ?? ''
  };
  if (versionMetadata) {
    meta.versionMetadata = versionMetadata;
  }
  const main = Object.fromEntries(Object.entries(v2Main).map(([key, value]) => {
    if (value === undefined) {
      return [key, null];
    }
    return [key, value];
  }));
  const data = {
    main,
    ...v2Data
  };
  const result = {
    files: csvFiles
  };
  const payload = {
    meta,
    data,
    result
  };
  return JSON.stringify(payload);
}
function extractCsvFiles(submitResponse) {
  const result = submitResponse.result;
  return {
    main: result.files?.main ?? '',
    repeaters: result.files?.repeaters ?? {}
  };
}
//# sourceMappingURL=v1.js.map