import { databaseTypes, fieldTypes } from '../../../lib/jhipster/index.js';
import { mutateData } from '../../base/support/index.js';
const { MYSQL, MARIADB } = databaseTypes;
const { CommonDBTypes, RelationalOnlyDBTypes, BlobTypes } = fieldTypes;
const { STRING, INTEGER, LONG, BIG_DECIMAL, FLOAT, DOUBLE, UUID, BOOLEAN, LOCAL_DATE, ZONED_DATE_TIME, INSTANT, DURATION, LOCAL_TIME } = CommonDBTypes;
const { BYTES } = RelationalOnlyDBTypes;
const { TEXT } = BlobTypes;
function parseLiquibaseColumnType(field) {
    const fieldType = field.fieldType;
    if (fieldType === STRING || field.fieldIsEnum) {
        return `varchar(${field.fieldValidateRulesMaxlength || 255})`;
    }
    if (fieldType === INTEGER) {
        return 'integer';
    }
    if (fieldType === LONG) {
        return 'bigint';
    }
    if (fieldType === FLOAT) {
        return '${floatType}';
    }
    if (fieldType === DOUBLE) {
        return 'double';
    }
    if (fieldType === BIG_DECIMAL) {
        return 'decimal(21,2)';
    }
    if (fieldType === LOCAL_DATE) {
        return 'date';
    }
    if (fieldType === INSTANT) {
        return '${datetimeType}';
    }
    if (fieldType === ZONED_DATE_TIME) {
        return '${datetimeType}';
    }
    if (fieldType === DURATION) {
        return 'bigint';
    }
    if (fieldType === LOCAL_TIME) {
        return '${timeType}';
    }
    if (fieldType === UUID) {
        return '${uuidType}';
    }
    if (fieldType === BYTES && field.fieldTypeBlobContent !== TEXT) {
        return '${blobType}';
    }
    if (field.fieldTypeBlobContent === TEXT) {
        return '${clobType}';
    }
    if (fieldType === BOOLEAN) {
        return 'boolean';
    }
    return undefined;
}
function parseLiquibaseLoadColumnType(application, field) {
    const columnType = field.columnType;
    if (['integer', 'bigint', 'double', 'decimal(21,2)', '${floatType}'].includes(columnType)) {
        return 'numeric';
    }
    if (field.fieldIsEnum) {
        return 'string';
    }
    if (['date', '${datetimeType}'].includes(columnType)) {
        return 'date';
    }
    if (columnType === '${timeType}') {
        return 'time';
    }
    if (columnType === 'boolean') {
        return columnType;
    }
    if (columnType === '${blobType}') {
        return 'blob';
    }
    if (columnType === '${clobType}') {
        return 'clob';
    }
    const { prodDatabaseType } = application;
    if (columnType === '${uuidType}' &&
        prodDatabaseType !== MYSQL &&
        prodDatabaseType !== MARIADB) {
        return '${uuidType}';
    }
    return 'string';
}
export default function prepareField(application, field) {
    mutateData(field, {
        __override__: false,
        columnType: data => parseLiquibaseColumnType(data),
        liquibaseDefaultValueAttributeValue: ({ defaultValue, defaultValueComputed }) => defaultValueComputed ?? defaultValue?.toString(),
        liquibaseDefaultValueAttributeName: ({ defaultValueComputed, liquibaseDefaultValueAttributeValue }) => {
            if (liquibaseDefaultValueAttributeValue === undefined)
                return undefined;
            if (defaultValueComputed)
                return 'defaultValueComputed';
            if (field.fieldTypeNumeric)
                return 'defaultValueNumeric';
            if (field.fieldTypeDateTime)
                return 'defaultValueDate';
            if (field.fieldTypeBoolean)
                return 'defaultValueBoolean';
            return 'defaultValue';
        },
        shouldDropDefaultValue: data => !data.liquibaseDefaultValueAttributeValue && (data.fieldType === ZONED_DATE_TIME || data.fieldType === INSTANT),
        shouldCreateContentType: data => data.fieldType === BYTES && data.fieldTypeBlobContent !== TEXT,
        columnRequired: data => data.nullable === false || (data.fieldValidate === true && data.fieldValidateRules?.includes('required')),
        nullable: data => !data.columnRequired,
        loadColumnType: data => parseLiquibaseLoadColumnType(application, data),
        liquibaseGenerateFakeData: true,
    });
    return field;
}
