"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.normalizeAlias = exports.FinancialSpreadingAlias = exports.FinancialSpreadingAliasSchema = void 0;
const mongoose_1 = __importDefault(require("mongoose"));
const _models_1 = require("./_models");
exports.FinancialSpreadingAliasSchema = new mongoose_1.default.Schema({
    sheetId: {
        type: mongoose_1.default.Schema.Types.ObjectId,
        ref: _models_1.MODEL_NAMES.financialSpreadingSheets,
        required: true,
    },
    aliases: [{
            original: {
                type: String,
                required: false,
                trim: true,
            },
            normalized: {
                type: String,
                required: false,
                trim: true,
            },
        }],
}, {
    timestamps: { createdAt: true, updatedAt: true },
});
exports.FinancialSpreadingAlias = mongoose_1.default.model(_models_1.MODEL_NAMES.financialSpreadingAlias, exports.FinancialSpreadingAliasSchema);
exports.FinancialSpreadingAliasSchema.index({
    'aliases.normalized': 1,
});
exports.FinancialSpreadingAliasSchema.pre('validate', function (next) {
    this.aliases = this.aliases
        .filter(a => a.original?.trim())
        .map(a => ({
        original: a.original.trim(),
        normalized: normalizeAlias(a.original),
    }));
    next();
});
function normalizeAlias(input) {
    return input
        .toLowerCase()
        .replace(/[–—−]/g, '-')
        .replace(/[_]/g, ' ')
        .replace(/[^\p{L}\p{N}\s-]/gu, '')
        .replace(/\s+/g, ' ')
        .trim();
}
exports.normalizeAlias = normalizeAlias;
