export function toFrameworkErrorModel(args) {
    return {
        kind: args.kind,
        severity: args.severity ?? "error",
        source: args.source,
        message: args.message,
        details: [...(args.details || [])],
        recoverable: args.recoverable === true,
        cause: args.cause,
    };
}
export function frameworkErrorFromUnknown(args) {
    const message = args.error instanceof Error && args.error.message.trim().length > 0
        ? args.error.message
        : String(args.error || "Unknown framework error");
    return toFrameworkErrorModel({
        kind: args.kind,
        source: args.source,
        message,
        recoverable: args.recoverable,
        cause: args.error,
    });
}
const COORDINATOR_PHASE_TO_KIND = {
    "coordinator-ready": "coordinator-init",
    "state-load": "tool-state-load",
    "state-save": "tool-state-save",
    "provider-register": "provider-register",
    "provider-init": "provider-init",
    "tts-init": "tts-init",
    "section-controller-init": "section-controller-init",
    "section-controller-dispose": "section-controller-dispose",
};
/**
 * Build a {@link FrameworkErrorModel} from a coordinator-side error +
 * context pair.
 *
 * Maps `context.phase` to the canonical {@link FrameworkErrorKind} and
 * synthesizes a `source` from `context.providerId` when present
 * (`pie-toolkit-coordinator/<providerId>`); falls back to a phase-tagged
 * source (`pie-toolkit-coordinator:<phase>`) otherwise. Forwards the
 * original `error` as `cause` so hosts that care about the underlying
 * `Error` keep getting it.
 *
 * Recoverable defaults to `false` (most coordinator-phase failures are
 * not auto-recovered today). Override with `recoverable: true` for the
 * phases where the coordinator continues operating after the failure.
 */
export function frameworkErrorFromCoordinatorContext(args) {
    const kind = COORDINATOR_PHASE_TO_KIND[args.context.phase];
    const source = args.context.providerId
        ? `pie-toolkit-coordinator/${args.context.providerId}`
        : `pie-toolkit-coordinator:${args.context.phase}`;
    return frameworkErrorFromUnknown({
        kind,
        source,
        error: args.error,
        recoverable: args.recoverable,
    });
}
export function frameworkErrorFromToolConfigDiagnostics(args) {
    const details = args.diagnostics.map((diagnostic) => `${diagnostic.path}: ${diagnostic.message}`);
    const message = details.length > 0
        ? "Invalid tools config."
        : "Invalid tools config (no diagnostics details were provided).";
    return toFrameworkErrorModel({
        kind: "tool-config",
        source: args.source,
        message,
        details,
        recoverable: args.recoverable,
    });
}
export function formatFrameworkErrorForConsole(error) {
    const prefix = `[pie-framework:${error.kind}:${error.source}]`;
    if (error.details.length === 0)
        return `${prefix} ${error.message}`;
    return `${prefix} ${error.message}\n- ${error.details.join("\n- ")}`;
}
//# sourceMappingURL=framework-error.js.map