import { config } from "../../../../../config/index.js";
import { getAnswer } from "../../components/helpers/components.js";
import { FileUploadField } from "../../components/index.js";
const designerUrl = config.get('designerUrl');
export function format(context, items, model, _submitResponse, _formStatus, _formMetadata) {
  const now = new Date();
  const categorisedData = categoriseData(items);
  const data = {
    meta: {
      schemaVersion: '1',
      timestamp: now.toISOString(),
      referenceNumber: context.referenceNumber,
      definition: model.def
    },
    data: categorisedData
  };
  const body = JSON.stringify(data);
  return body;
}

/**
 * Categories the form submission data into the "main" body and "repeaters".
 *
 * {
 *    main: {
 *       componentName: 'componentValue',
 *    },
 *    repeaters: {
 *      repeaterName: [
 *        {
 *          componentName: 'componentValue'
 *        }
 *      ]
 *    },
 *    files: {
 *      fileComponentName: [
 *        {
 *          fileId: '123-456-789',
 *          link: 'https://forms-designer/file-download/123-456-789'
 *        }
 *      ]
 *    }
 * }
 */
function categoriseData(items) {
  const output = {
    main: {},
    repeaters: {},
    files: {}
  };
  items.forEach(item => {
    if ('subItems' in item) {
      output.repeaters[item.name] = extractRepeaters(item);
    } else if (isFileUploadFieldItem(item)) {
      output.files[item.name] = extractFileUploads(item);
    } else {
      output.main[item.name] = getAnswer(item.field, item.state, {
        format: 'data'
      });
    }
  });
  return output;
}

/**
 * Returns the "repeaters" section of the response body
 * @param item - the repeater item
 * @returns the repeater item
 */
function extractRepeaters(item) {
  const repeaters = [];
  item.subItems.forEach(inputRepeaterItem => {
    const outputRepeaterItem = {};
    inputRepeaterItem.forEach(repeaterComponent => {
      outputRepeaterItem[repeaterComponent.name] = getAnswer(repeaterComponent.field, repeaterComponent.state, {
        format: 'data'
      });
    });
    repeaters.push(outputRepeaterItem);
  });
  return repeaters;
}

/**
 * Returns the "files" section of the response body
 * @param item - the file upload item in the form
 * @returns the file upload data
 */
function extractFileUploads(item) {
  const fileUploadState = item.field.getContextValueFromState(item.state) ?? [];
  return fileUploadState.map(fileId => {
    return {
      fileId,
      userDownloadLink: `${designerUrl}/file-download/${fileId}`
    };
  });
}
function isFileUploadFieldItem(item) {
  return item.field instanceof FileUploadField;
}

/**
 * A detail item specifically for files
 */
//# sourceMappingURL=v1.js.map