"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateDataModelDecoder = generateDataModelDecoder;
const Generator = __importStar(require("@postxl/generator"));
/**
 * Generates the data decoder, aggregating all the models.
 */
function generateDataModelDecoder({ context: { decoders, models } }) {
    const { dataModel, excelDataModel } = decoders;
    const imports = Generator.ImportGenerator.from(dataModel.decoder.zodDecoder.location);
    const encodedTypeEntries = [];
    const decodedTypeEntries = [];
    const encoderEntries = [];
    const decoderEntries = [];
    const encodedExcelTypeEntries = [];
    const encoderExcelEntries = [];
    const decoderExcelEntries = [];
    const transformerExcelEntries = [];
    for (const model of models.values()) {
        imports.addType(model.decoder.flattened.encodedType);
        encodedTypeEntries.push(`${model.decoder.dataModelEntry}: ${model.decoder.flattened.encodedType.name}[] | undefined`);
        imports.addType(model.decoder.excel.encodedType);
        encodedExcelTypeEntries.push(`${model.excelName}: ${model.decoder.excel.encodedType.name}[] | undefined`);
        imports.addType(model.types);
        decodedTypeEntries.push(`${model.decoder.dataModelEntry}?: ${model.types.name}[]`);
        imports.add(model.decoder.flattened.encoder);
        encoderEntries.push(`${model.decoder.dataModelEntry}: input.${model.decoder.dataModelEntry}?.map(${model.decoder.flattened.encoder.name}) ?? undefined`);
        imports.add(model.decoder.excel.encoder);
        encoderExcelEntries.push(`${model.excelName}: input.${model.decoder.dataModelEntry}?.map(${model.decoder.excel.encoder.name}) ?? undefined`);
        imports.add(model.decoder.flattened.decoder.zodDecoder);
        decoderEntries.push(`${model.decoder.dataModelEntry}: z.array(${model.decoder.flattened.decoder.zodDecoder.name}).optional()`);
        imports.add(model.decoder.excel.decoder.zodDecoder);
        decoderExcelEntries.push(`${model.excelName}: z.array(${model.decoder.excel.decoder.zodDecoder.name}).optional()`);
        transformerExcelEntries.push(`${model.decoder.dataModelEntry}: v.${model.excelName} ?? []`);
    }
    return `
import * as z from 'zod'
${imports.generate()}

/**
 * All data from the data model exported as a flattened object
 */
export type ${dataModel.encodedType.name} = {
  ${encodedTypeEntries.join(',\n')}
}

/**
 * All data after decoding an imported flattened object
 */
export type ${dataModel.decodedType.name} = {
  ${decodedTypeEntries.join(',\n')}
}

export function ${dataModel.encoder.name}(input: ${dataModel.decodedType.name}): ${dataModel.encodedType.name} {
  return {
    ${encoderEntries.join(',\n')}
  }
}

/**
 * Decoder that converts flattened model data to model data
 */
export const ${dataModel.decoder.zodDecoder.name} = z
  .object({
    ${decoderEntries.join(',\n')}
  })

export function ${dataModel.decoder.parseFunction.name}(input: ${dataModel.encodedType.name}): ${dataModel.decodedType.name} {
  return ${dataModel.decoder.zodDecoder.name}.parse(input)
}

/**
 * All data from the data model exported as a flattened Excel object
 */
export type ${excelDataModel.encodedType.name} = {
  ${encodedExcelTypeEntries.join(',\n')}
}

export function ${excelDataModel.encoder.name}(input: ${dataModel.decodedType.name}): ${excelDataModel.encodedType.name} {
  return {
    ${encoderExcelEntries.join(',\n')}
  }
}

/**
 * Decoder that converts flattened Excel model data to model data
 */
export const ${excelDataModel.decoder.zodDecoder.name} = z
  .object({
    ${decoderExcelEntries.join(',\n')}
  })
  .transform((v) => ({
    ${transformerExcelEntries.join(',\n')}
  }))

export function ${excelDataModel.decoder.parseFunction.name}(input: ${excelDataModel.encodedType.name}): ${dataModel.decodedType.name} {
  return ${excelDataModel.decoder.zodDecoder.name}.parse(input)
}
`;
}
//# sourceMappingURL=datamodel-decoder.generator.js.map