"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.getFieldElements = getFieldElements;
const Generator = __importStar(require("@postxl/generator"));
const utils_1 = require("@postxl/utils");
const utilsLib = Generator.toBackendModuleLocation('@postxl/utils');
const excelDecoders = {
    string: { name: Generator.toFunctionName('excelStringDecoder'), location: utilsLib },
    stringNullable: { name: Generator.toFunctionName('excelStringNullableDecoder'), location: utilsLib },
    number: { name: Generator.toFunctionName('excelNumberDecoder'), location: utilsLib },
    numberNullable: { name: Generator.toFunctionName('excelNumberNullableDecoder'), location: utilsLib },
    boolean: { name: Generator.toFunctionName('excelBooleanDecoder'), location: utilsLib },
    booleanNullable: { name: Generator.toFunctionName('excelBooleanNullableDecoder'), location: utilsLib },
    date: { name: Generator.toFunctionName('excelDateDecoder'), location: utilsLib },
    dateNullable: { name: Generator.toFunctionName('excelDateNullableDecoder'), location: utilsLib },
};
function getFieldElements(params) {
    switch (params.field.kind) {
        case 'id':
            return getFieldElementsForId(params);
        case 'scalar':
            return getFieldElementsForScalar(params);
        case 'relation':
            return getFieldElementsForRelation(params);
        case 'enum':
            return getFieldElementsForEnum(params);
        case 'discriminatedUnion':
            return getFieldElementsForDiscriminatedUnion(params);
        default:
            throw new utils_1.ExhaustiveSwitchCheck(params.field);
    }
}
function getFieldElementsForId({ model, imports, }) {
    imports.add(model.types.id.toBranded);
    imports.add(excelDecoders.string);
    return {
        rawDecoder: excelDecoders.string.name,
        transformer: (name) => `${model.types.id.toBranded.name}(${name})`,
    };
}
function getFieldElementsForScalar(params) {
    const enforceNullable = params.enforceNullable || params.field.isCreatedAt || params.field.isUpdatedAt;
    const elements = getFieldElementsForScalar_Base({ ...params, enforceNullable });
    if (params.field.isCreatedAt || params.field.isUpdatedAt) {
        elements.rawDecoder += `.optional()`;
        elements.transformer = (name) => `${name} ?? new Date()`;
    }
    return elements;
}
function getFieldElementsForScalar_Base(params) {
    switch (params.field.type) {
        case 'number':
            return getFieldElementsForScalarNumber(params);
        case 'string':
            return getFieldElementsForScalarString(params);
        case 'boolean':
            return getFieldElementsForScalarBoolean(params);
        case 'Date':
            return getFieldElementsForScalarDate(params);
        default:
            throw new utils_1.ExhaustiveSwitchCheck(params.field);
    }
}
function getFieldElementsForScalarNumber({ field, imports, enforceNullable, }) {
    if (field.validations.type === 'int' || field.databaseType === 'Int' || field.databaseType === 'BigInt') {
        // TODO: Add int validation
    }
    if (field.validations.min !== undefined) {
        // TODO: Add min validation
    }
    if (field.validations.max !== undefined) {
        // TODO: Add max validation
    }
    if (!field.isRequired || enforceNullable) {
        imports.add(excelDecoders.numberNullable);
        return { rawDecoder: excelDecoders.numberNullable.name };
    }
    imports.add(excelDecoders.number);
    return { rawDecoder: excelDecoders.number.name };
}
function getFieldElementsForScalarString({ field, imports, enforceNullable, }) {
    if (field.validations.maxLength) {
        // TODO: Add maxLength validation
    }
    if (!field.isRequired || enforceNullable) {
        imports.add(excelDecoders.stringNullable);
        return { rawDecoder: excelDecoders.stringNullable.name };
    }
    imports.add(excelDecoders.string);
    return { rawDecoder: excelDecoders.string.name };
}
function getFieldElementsForScalarBoolean({ field, enforceNullable, imports, }) {
    if (!field.isRequired || enforceNullable) {
        imports.add(excelDecoders.booleanNullable);
        return { rawDecoder: excelDecoders.booleanNullable.name };
    }
    imports.add(excelDecoders.boolean);
    return { rawDecoder: excelDecoders.boolean.name };
}
function getFieldElementsForScalarDate({ field, enforceNullable, imports, }) {
    if (!field.isRequired || enforceNullable) {
        imports.add(excelDecoders.dateNullable);
        return { rawDecoder: excelDecoders.dateNullable.name };
    }
    imports.add(excelDecoders.date);
    return { rawDecoder: excelDecoders.date.name };
}
function getFieldElementsForRelation({ field, context, imports, enforceNullable, }) {
    const referencedModel = context.models.get(field.referencedModelName);
    imports.add(referencedModel.types.id.toBranded);
    if (!field.isRequired || enforceNullable) {
        imports.add(excelDecoders.stringNullable);
    }
    else {
        imports.add(excelDecoders.string);
    }
    const rawDecoder = !field.isRequired || enforceNullable ? excelDecoders.stringNullable.name : excelDecoders.string.name;
    const transformer = field.isRequired
        ? (name) => `${referencedModel.types.id.toBranded.name}(${name})`
        : (name) => `${name} === null ? null : ${referencedModel.types.id.toBranded.name}(${name})`;
    return { rawDecoder, transformer };
}
function getFieldElementsForEnum({ field, context, imports, enforceNullable, }) {
    const enum_ = context.enums.get(field.enumName);
    imports.add(enum_.decoder.decoder.rawInputValidation).add(enum_.decoder.decoder.transformer);
    const rawDecoder = !field.isRequired || enforceNullable
        ? `${enum_.decoder.decoder.rawInputValidation.name}.nullable()`
        : `${enum_.decoder.decoder.rawInputValidation.name}`;
    const transformer = field.isRequired
        ? (name) => `${enum_.decoder.decoder.transformer.name}(${name})`
        : (name) => `${name} === null ? null : ${enum_.decoder.decoder.transformer.name}(${name})`;
    return { rawDecoder, transformer };
}
function getFieldElementsForDiscriminatedUnion({ field, model, imports, }) {
    const du = model.decoder.discriminatedUnions.get(field.name);
    imports.add(du.flattened.decoder.rawInputValidation).add(du.flattened.decoder.transformer);
    const transformer = field.isRequired
        ? (name) => `${du.flattened.decoder.transformer.name}(${name})`
        : (name) => `${name}.${field.name} === null ? null : ${du.flattened.decoder.transformer.name}(${name})`;
    return {
        rawDecoder: `${du.flattened.decoder.rawInputValidation.name}`,
        transformer,
    };
}
//# sourceMappingURL=excel-field-decoder.helper.js.map