"use strict";
/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
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;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CompileSingleData = exports.SingleData = exports.RECORDE_MODULE_NODE = exports.RECORDE_COMPILE_NODE = exports.RECORDE_RUN_NODE = exports.BS_PERF_FILE_NAME = void 0;
const fs = __importStar(require("fs"));
const path_1 = __importDefault(require("path"));
const types_1 = require("../types");
exports.BS_PERF_FILE_NAME = 'bs_record_perf.csv';
var RECORDE_RUN_NODE;
(function (RECORDE_RUN_NODE) {
    RECORDE_RUN_NODE["GEN_MODULE"] = "run generateModuleInfos";
    RECORDE_RUN_NODE["COMPILE_FILES"] = "run compileMultiFiles";
    RECORDE_RUN_NODE["END"] = "run end";
})(RECORDE_RUN_NODE || (exports.RECORDE_RUN_NODE = RECORDE_RUN_NODE = {}));
var RECORDE_COMPILE_NODE;
(function (RECORDE_COMPILE_NODE) {
    RECORDE_COMPILE_NODE["PROCEED_PARSE"] = "compileMultiFiles proceedToState parsed";
    RECORDE_COMPILE_NODE["PLUGIN_PARSE"] = "compileMultiFiles plugin parsed";
    RECORDE_COMPILE_NODE["PROCEED_CHECK"] = "compileMultiFiles proceedToState checked";
    RECORDE_COMPILE_NODE["PLUGIN_CHECK"] = "compileMultiFiles plugin checked";
    RECORDE_COMPILE_NODE["BIN_GENERATE"] = "compileMultiFiles bin generated";
    RECORDE_COMPILE_NODE["CFG_DESTROY"] = "compileMultiFiles config destroyed";
    RECORDE_COMPILE_NODE["END"] = "compileMultiFiles end";
})(RECORDE_COMPILE_NODE || (exports.RECORDE_COMPILE_NODE = RECORDE_COMPILE_NODE = {}));
var RECORDE_MODULE_NODE;
(function (RECORDE_MODULE_NODE) {
    RECORDE_MODULE_NODE["COLLECT_INFO"] = "generateModuleInfos collectModuleInfos";
    RECORDE_MODULE_NODE["GEN_CONFIG"] = "generateModuleInfos generateArkTSConfigForModules";
    RECORDE_MODULE_NODE["CLT_FILES"] = "generateModuleInfos collectCompileFiles";
    RECORDE_MODULE_NODE["SAVE_CACHE"] = "generateModuleInfos saveHashCache";
    RECORDE_MODULE_NODE["END"] = "generateModuleInfos end";
})(RECORDE_MODULE_NODE || (exports.RECORDE_MODULE_NODE = RECORDE_MODULE_NODE = {}));
class SingleData {
    constructor() {
        this.time = 0;
        this.mem = 0;
    }
}
exports.SingleData = SingleData;
class CompileSingleData {
    constructor(file, recordType) {
        this.startTime = 0;
        this.startMem = 0;
        this.file = '';
        this.file = file;
        this.timeMemMap = new Map();
        // close by default
        this.recordType = recordType ?? types_1.RECORD_TYPE.DEFAULT_TYPE;
    }
    record(startKey, lastEndKey = '') {
        if (this.recordType === types_1.RECORD_TYPE.DEFAULT_TYPE) {
            return;
        }
        let currentTime = new Date().getTime();
        let currentMem = process.memoryUsage.rss();
        let tmp = this.timeMemMap.get(lastEndKey);
        if (tmp) {
            tmp.time = currentTime - this.startTime;
            tmp.mem = (currentMem > this.startMem) ? (currentMem - this.startMem) : 0;
            this.timeMemMap.set(lastEndKey, tmp);
        }
        if (startKey === '') {
            return;
        }
        if (this.timeMemMap.get(startKey) !== undefined) {
            return;
        }
        this.startTime = currentTime;
        this.startMem = currentMem;
        let data = new SingleData();
        data.time = 0;
        data.mem = 0;
        this.timeMemMap.set(startKey, data);
    }
    writeSumSingle(cachePath, deputyName = '') {
        if (this.recordType === types_1.RECORD_TYPE.DEFAULT_TYPE) {
            return;
        }
        const csvData = [
            'timeKey, time(ms), mem(M)'
        ];
        this.timeMemMap.forEach((v, k) => {
            let element = `${k}` + ', ' + `${v.time}` + 'ms' + ', ' + `${Math.round(v.mem / 1024 / 1024)}` + 'M';
            csvData.push(element);
        });
        let name = path_1.default.basename(this.file);
        let currentExt = path_1.default.extname(name);
        let fileWithoutExt = name.substring(0, name.lastIndexOf(currentExt));
        let fileName = `${fileWithoutExt}` + deputyName + '.csv';
        let filePath = path_1.default.join(cachePath, fileName);
        csvData.forEach(row => {
            fs.appendFileSync(filePath, `${row}\n`);
        });
    }
}
exports.CompileSingleData = CompileSingleData;
