"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;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
const types_1 = require("../types");
const logger_1 = require("../logger");
const error_1 = require("../util/error");
const ets2panda_1 = require("../util/ets2panda");
const utils_1 = require("../util/utils");
const path = __importStar(require("path"));
const pre_define_1 = require("../pre_define");
const logger = logger_1.Logger.getInstance(logger_1.getConsoleLogger);
function declgen(id, task) {
    const ets2panda = ets2panda_1.Ets2panda.getInstance(task.buildConfig);
    try {
        ets2panda.initalize();
        if (task.fileList.length > 1) {
            for (const file of task.fileList) {
                const outPut = path.resolve(task.buildConfig.cachePath, task.fileInfo.moduleName, (0, utils_1.changeFileExtension)(path.relative(task.fileInfo.moduleRoot, file), pre_define_1.ABC_SUFFIX));
                const subTask = {
                    ...task,
                    fileInfo: {
                        input: file,
                        output: outPut,
                        arktsConfig: task.fileInfo.arktsConfig,
                        moduleName: task.fileInfo.moduleName,
                        moduleRoot: task.fileInfo.moduleRoot,
                    },
                    fileList: [file]
                };
                ets2panda.declgenV1(subTask, subTask.buildConfig.skipDeclCheck ?? true, subTask.buildConfig.genDeclAnnotations ?? true);
            }
        }
        else {
            ets2panda.declgenV1(task, task.buildConfig.skipDeclCheck ?? true, task.buildConfig.genDeclAnnotations ?? true);
        }
        process.send({
            type: types_1.WorkerMessageType.DECL_GENERATED,
            data: {
                taskId: id,
            }
        });
    }
    catch (error) {
        if (error instanceof error_1.DriverError) {
            process.send({
                type: types_1.WorkerMessageType.ERROR_OCCURED,
                data: {
                    taskId: id,
                    error: error.logData
                }
            });
        }
    }
    finally {
        ets2panda.finalize();
    }
}
process.on('message', (message) => {
    const { type, data } = message;
    logger.printDebug(`Got message from parent. Type: ${type}`);
    logger.printDebug(`Got message from parent. payload: ${JSON.stringify(data, null, 1)}`);
    try {
        switch (type) {
            case types_1.WorkerMessageType.ASSIGN_TASK:
                declgen(data.taskId, data.payload);
                break;
            default:
                break;
        }
    }
    catch (error) {
        logger.printDebug('Error occured');
        if (error instanceof Error) {
            let logData = logger_1.LogDataFactory.newInstance(error_1.ErrorCode.BUILDSYSTEM_COMPILE_ABC_FAIL, 'Compile abc files failed.', error.message);
            process.send({
                type: types_1.WorkerMessageType.ERROR_OCCURED,
                data: {
                    taskId: data.taskId,
                    error: logData
                }
            });
        }
        logger.printDebug('Sent the error to the parent');
    }
});
