"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
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.grp2025 = exports.grp2024 = exports.grp = void 0;
var fs = __importStar(require("fs"));
var path = __importStar(require("path"));
var utils_1 = require("../utils");
var rum_1 = require("../utils/rum");
var error_management_1 = require("./error-management");
var tryPath = function () {
    var parts = [];
    for (var _i = 0; _i < arguments.length; _i++) {
        parts[_i] = arguments[_i];
    }
    var fullPath = path.resolve.apply(path, parts);
    if (!fs.existsSync(fullPath)) {
        throw new Error("Native module not found at ".concat(fullPath));
    }
    return fullPath;
};
var grp2025Path = tryPath(__dirname, '..', '..', 'build', 'Release', 'grp2025.node');
if (!fs.existsSync(grp2025Path)) {
    throw new Error("Native module not found 2 at ".concat(grp2025Path));
}
var grp2025Native = require(grp2025Path);
// const grp2025Native = require('bindings')('grp2025')
var grp2024Native = require('bindings')('grp2024');
var ficumNative = require('bindings')('ficum');
// Resolve paths relative to this package, not the consuming project
var getTablePath = function (version) {
    try {
        // Find the package root by looking for package.json
        var packageJsonPath = require.resolve('@beparallel/grouping/package.json');
        var packageRoot = path.dirname(packageJsonPath);
        return path.join(packageRoot, 'tables', version) + path.sep;
    }
    catch (error) {
        // Fallback for development or when require.resolve fails
        var packageRoot = path.resolve(__dirname, '../../');
        return path.join(packageRoot, 'tables', version) + path.sep;
    }
};
// Wrapper for ficum with automatic table path resolution
var createFicumWrapper = function () {
    return {
        main: function (ficuminPath, ficumoutPath, version, log, gestionPortee, finessGeo) {
            var dirtables = getTablePath("V".concat(version));
            return ficumNative.main(ficuminPath, ficumoutPath, dirtables, log !== null && log !== void 0 ? log : 0, gestionPortee !== null && gestionPortee !== void 0 ? gestionPortee : 1, finessGeo !== null && finessGeo !== void 0 ? finessGeo : 1);
        }
    };
};
// Extract the actual wrapper functions from the native modules
var grp2025 = function (input) {
    var rumStrings = input.rumObjects.map(function (rumObject) { return (0, utils_1.parseRUMObjectTo022FormatString)(rumObject); });
    var dirtab = getTablePath('V2025');
    var ns = rumStrings.length;
    var result = grp2025Native.grp_2025_wrapper(rumStrings, dirtab, ns, input.typeEtab);
    var mappedError = (0, error_management_1.mapGroupingError)(result.returnCode);
    return __assign(__assign({}, result), { returnError: mappedError });
};
exports.grp2025 = grp2025;
var grp2024 = function (input) {
    var rumStrings = input.rumObjects.map(function (rumObject) { return (0, utils_1.parseRUMObjectTo022FormatString)(rumObject); });
    var dirtab = getTablePath('V2024');
    var ns = rumStrings.length;
    var result = grp2024Native.grp_2024_wrapper(rumStrings, dirtab, ns, input.typeEtab);
    var mappedError = (0, error_management_1.mapGroupingError)(result.returnCode);
    return __assign(__assign({}, result), { returnError: mappedError });
};
exports.grp2024 = grp2024;
var ficum = createFicumWrapper();
var grp = function (input) {
    var rumObjects = input.rumObjects;
    if ((0, rum_1.isRumObjectsLinkedToYear)(rumObjects, 2025)) {
        return grp2025(input);
    }
    else if ((0, rum_1.isRumObjectsLinkedToYear)(rumObjects, 2024)) {
        return grp2024(input);
    }
    console.warn('No year found in rumObjects, using 2025 version');
    return grp2025(input);
};
exports.grp = grp;
