"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigFile = void 0;
const papaparse_1 = require("papaparse");
const parameter_1 = require("./parameter");
/**
 * Parses the contents of configuration files such as config.csv, .factory_config, and .assembly_config.
 */
class ConfigFile {
    constructor(content, file) {
        var _a, _b;
        this.content = content;
        this.file = file;
        const normalizedContent = content.replace(/[\r\n]+/g, '\n');
        this.parseResult = (0, papaparse_1.parse)(normalizedContent, {
            skipEmptyLines: true,
            transform: (value) => value.trim(),
        });
        if (((_b = (_a = this.parseResult) === null || _a === void 0 ? void 0 : _a.errors) === null || _b === void 0 ? void 0 : _b.length) > 0) {
            throw new Error('Failed to parse the contents of the configuration CSV file.');
        }
        const matchApiId = normalizedContent.match(/#\s+(\d{4}-\d{2}(-\d{4})?(-\d+)*)$/m);
        if (matchApiId) {
            this.apiId = matchApiId[1];
        }
        const matchFirmwareVersion = normalizedContent.match(/^#\s+(v.*)$/m);
        if (matchFirmwareVersion) {
            this.firmwareVersion = matchFirmwareVersion[1];
        }
        const matchFirstParameterRow = normalizedContent.match(/^0x\d{4}/m);
        if (matchFirstParameterRow === null || matchFirstParameterRow === void 0 ? void 0 : matchFirstParameterRow.index) {
            this.header = normalizedContent.slice(0, matchFirstParameterRow.index - 1);
        }
        this.parameters = this.parseResult.data
            .filter(([c0]) => c0.startsWith('0x'))
            .map(([c0, c1, c2]) => {
            const index = parseInt(c0, 16);
            const subindex = parseInt(c1, 10);
            const numValue = Number(c2);
            const value = isNaN(numValue) ? c2 : numValue;
            return { index, subindex, value };
        })
            .sort(parameter_1.parametersCompareFn);
    }
    get firmwareId() {
        return this.apiId;
    }
    get isSmmConfigFile() {
        for (const parameter of this.parameters) {
            if ((0, parameter_1.isSmmParameterIndex)(parameter.index)) {
                return true;
            }
        }
        return false;
    }
    get hasParseErrors() {
        var _a, _b;
        return ((_b = (_a = this.parseResult) === null || _a === void 0 ? void 0 : _a.errors.length) !== null && _b !== void 0 ? _b : 0) > 0;
    }
    get numberOfParameters() {
        var _a, _b;
        return (_b = (_a = this.parameters) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
    }
    toString() {
        let out = this.parameters
            .map((p) => { var _a, _b; return `0x${(_a = p.index) === null || _a === void 0 ? void 0 : _a.toString(16)}, ${(_b = p.subindex) === null || _b === void 0 ? void 0 : _b.toString(10)}, ${p.value}`; })
            .join('\n');
        if (this.header) {
            out = this.header + '\n' + out;
        }
        return out + '\n';
    }
}
exports.ConfigFile = ConfigFile;
//# sourceMappingURL=config-file.js.map