"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.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleCompileProcessWorkerExit = handleCompileProcessWorkerExit;
exports.handleDeclgenWorkerExit = handleDeclgenWorkerExit;
const error_1 = require("../util/error");
const process_build_config_1 = require("../init/process_build_config");
const logger_1 = require("../logger");
function getDeclgenErrorMessage(workerInfo, task, code, signal) {
    if (signal) {
        switch (signal) {
            case 'SIGSEGV':
                return `Declgen Worker caught SIGSEGV signal. Processed file is ${task.payload.fileInfo.input}.`;
            case 'SIGKILL':
                return `Declgen Worker was killed by signal ${signal}`;
            default:
                return `Signal ${signal} was sent to the declgen worker. Processed file is ${task.payload.fileInfo.input}.`;
        }
    }
    if (code && code !== 0) {
        return `Declgen worker crashed when process file ${task.payload.fileInfo.input}. Exit code ${code}(0x${code.toString(16)})`;
    }
    return `Worker [ID:${workerInfo.id}] exited unexpectedly`;
}
function getCompileErrorMessage(workerInfo, task, code, signal) {
    if (signal) {
        switch (signal) {
            case 'SIGSEGV':
                return `Failed to compile file ${task.payload.fileInfo.input}. Compiler worker caught SIGSEGV signal.`;
            case 'SIGKILL':
                return `Failed to compile file ${task.payload.fileInfo.input}. Compiler worker was killed by signal ${signal}`;
            default:
                return `Signal ${signal} was sent to the compiler worker. Processed file is ${task.payload.fileInfo.input}.`;
        }
    }
    if (code && code !== 0) {
        return `Failed to compile file ${task.payload.fileInfo.input}. Exit code ${code}(0x${code.toString(16)})`;
    }
    return `Worker [ID:${workerInfo.id}] exited unexpectedly`;
}
function handleCompileProcessWorkerExit(workerInfo, task, code, signal) {
    const es2pandPath = (0, process_build_config_1.getEs2pandaPath)(task.payload.buildConfig);
    const cmd = [
        es2pandPath,
        '--arktsconfig', task.payload.fileInfo.arktsConfig,
        '--output', task.payload.fileInfo.output,
        task.payload.fileInfo.input
    ];
    return logger_1.LogDataFactory.newInstance(error_1.ErrorCode.BUILDSYSTEM_COMPILE_FAILED_IN_WORKER, getCompileErrorMessage(workerInfo, task, code, signal), 'This error is likely caused internally from compiler.', task.payload.fileList[0], [`Run locally: ${cmd.join(' ')}`]);
}
function handleDeclgenWorkerExit(workerInfo, task, code, signal) {
    return logger_1.LogDataFactory.newInstance(error_1.ErrorCode.BUILDSYSTEM_DECLGEN_FAILED_IN_WORKER, getDeclgenErrorMessage(workerInfo, task, code, signal), 'This error is likely caused internally from compiler.');
}
