"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WorkerTimeoutError = exports.WorkerUnavailableError = exports.WorkerAuthInvalidError = exports.WorkerQuotaExhaustedError = exports.WorkerDecisionParseError = exports.WorkerExecutionError = exports.WorkerFailureError = exports.WORKER_FAILURE_KINDS = exports.WORKER_TIMEOUT_MESSAGE = exports.WORKER_CAPACITY_MESSAGE = exports.WORKER_AUTH_INVALID_MESSAGE = exports.WORKER_QUOTA_EXHAUSTED_MESSAGE = exports.WORKER_DECISION_PAYLOAD_INVALID_MESSAGE = exports.WORKER_DECISION_BLOCK_MISSING_MESSAGE = exports.WORKER_EXECUTION_FAILED_MESSAGE = void 0;
exports.isWorkerFailureError = isWorkerFailureError;
exports.hasAttemptedWorkerExecution = hasAttemptedWorkerExecution;
exports.readWorkerFailureDurationMs = readWorkerFailureDurationMs;
const catalog_1 = require("../i18n/catalog");
exports.WORKER_EXECUTION_FAILED_MESSAGE = (0, catalog_1.translate)(catalog_1.DEFAULT_APP_LOCALE, "worker.error.executionFailed");
exports.WORKER_DECISION_BLOCK_MISSING_MESSAGE = (0, catalog_1.translate)(catalog_1.DEFAULT_APP_LOCALE, "worker.error.decisionBlockMissing");
exports.WORKER_DECISION_PAYLOAD_INVALID_MESSAGE = (0, catalog_1.translate)(catalog_1.DEFAULT_APP_LOCALE, "worker.error.decisionPayloadInvalid");
exports.WORKER_QUOTA_EXHAUSTED_MESSAGE = (0, catalog_1.translate)(catalog_1.DEFAULT_APP_LOCALE, "worker.error.quotaExhausted");
exports.WORKER_AUTH_INVALID_MESSAGE = (0, catalog_1.translate)(catalog_1.DEFAULT_APP_LOCALE, "worker.error.authInvalid");
exports.WORKER_CAPACITY_MESSAGE = (0, catalog_1.translate)(catalog_1.DEFAULT_APP_LOCALE, "worker.error.capacity");
exports.WORKER_TIMEOUT_MESSAGE = (0, catalog_1.translate)(catalog_1.DEFAULT_APP_LOCALE, "worker.error.timeout");
exports.WORKER_FAILURE_KINDS = {
    quotaExhausted: "quota_exhausted",
    authInvalid: "auth_invalid",
    workerUnavailable: "worker_unavailable",
    timeout: "timeout",
    decisionInvalid: "decision_invalid",
    executionFailed: "execution_failed",
};
class WorkerFailureError extends Error {
    failureKind;
    publicMessage;
    diagnosticMessage;
    attempted;
    cooldownUntil;
    workerDurationMs;
    constructor(name, failureKind, publicMessageKey, diagnosticMessage, options) {
        const locale = options?.locale ?? catalog_1.DEFAULT_APP_LOCALE;
        const defaultMessage = (0, catalog_1.translate)(locale, publicMessageKey);
        super(diagnosticMessage ?? defaultMessage, options);
        this.name = name;
        this.failureKind = failureKind;
        this.publicMessage = options?.publicMessage ?? defaultMessage;
        this.diagnosticMessage = diagnosticMessage ?? defaultMessage;
        this.attempted = options?.attempted ?? false;
        this.cooldownUntil = options?.cooldownUntil ?? null;
        this.workerDurationMs = options?.workerDurationMs ?? 0;
    }
}
exports.WorkerFailureError = WorkerFailureError;
class WorkerExecutionError extends WorkerFailureError {
    constructor(diagnosticMessage, options) {
        super("WorkerExecutionError", exports.WORKER_FAILURE_KINDS.executionFailed, "worker.error.executionFailed", diagnosticMessage, options);
    }
}
exports.WorkerExecutionError = WorkerExecutionError;
class WorkerDecisionParseError extends WorkerFailureError {
    decisionBlock;
    validationIssues;
    issues;
    repairEligible;
    constructor(diagnosticMessage, options) {
        super("WorkerDecisionParseError", exports.WORKER_FAILURE_KINDS.decisionInvalid, "worker.error.decisionInvalid", diagnosticMessage, options);
        this.decisionBlock = options?.decisionBlock ?? null;
        this.issues = options?.issues ?? options?.validationIssues ?? [];
        this.validationIssues = options?.validationIssues ?? this.issues;
        this.repairEligible = options?.repairEligible ?? false;
    }
}
exports.WorkerDecisionParseError = WorkerDecisionParseError;
class WorkerQuotaExhaustedError extends WorkerFailureError {
    constructor(diagnosticMessage, options) {
        super("WorkerQuotaExhaustedError", exports.WORKER_FAILURE_KINDS.quotaExhausted, "worker.error.quotaExhausted", diagnosticMessage, options);
    }
}
exports.WorkerQuotaExhaustedError = WorkerQuotaExhaustedError;
class WorkerAuthInvalidError extends WorkerFailureError {
    constructor(diagnosticMessage, options) {
        super("WorkerAuthInvalidError", exports.WORKER_FAILURE_KINDS.authInvalid, "worker.error.authInvalid", diagnosticMessage, options);
    }
}
exports.WorkerAuthInvalidError = WorkerAuthInvalidError;
class WorkerUnavailableError extends WorkerFailureError {
    constructor(diagnosticMessage, options) {
        super("WorkerUnavailableError", exports.WORKER_FAILURE_KINDS.workerUnavailable, "worker.error.unavailable", diagnosticMessage, options);
    }
}
exports.WorkerUnavailableError = WorkerUnavailableError;
class WorkerTimeoutError extends WorkerFailureError {
    timeoutKind;
    constructor(diagnosticMessage, options) {
        super("WorkerTimeoutError", exports.WORKER_FAILURE_KINDS.timeout, "worker.error.timeout", diagnosticMessage, options);
        this.timeoutKind = options?.timeoutKind ?? null;
    }
}
exports.WorkerTimeoutError = WorkerTimeoutError;
function isWorkerFailureError(error) {
    return error instanceof WorkerFailureError;
}
function hasAttemptedWorkerExecution(error) {
    return isWorkerFailureError(error) ? error.attempted : false;
}
function readWorkerFailureDurationMs(error) {
    return isWorkerFailureError(error) ? error.workerDurationMs : 0;
}
