function asObject(value) {
    return value && typeof value === "object" && !Array.isArray(value)
        ? value
        : {};
}
function asString(value, fallback) {
    return typeof value === "string" && value.trim() ? value.trim() : fallback;
}
function asNumber(value, fallback, min, max) {
    if (typeof value !== "number" || !Number.isFinite(value))
        return fallback;
    return Math.max(min, Math.min(max, Math.floor(value)));
}
function asBool(value, fallback) {
    return typeof value === "boolean" ? value : fallback;
}
export function resolveFridayNextConfig(cfg) {
    const root = asObject(cfg);
    const channels = asObject(root.channels);
    const section = asObject(channels["friday-next"]);
    const sse = asObject(section.sse);
    const cors = asObject(section.cors);
    const authToken = asString(asObject(root.gateway).auth && asObject(asObject(root.gateway).auth).token, "") ||
        asString(section.authToken, "") ||
        asString(process.env.FRIDAY_NEXT_AUTH_TOKEN, "");
    return {
        channelId: "friday-next",
        pathPrefix: asString(section.pathPrefix, "/friday-next"),
        transport: asString(section.transport, "http+sse"),
        historyLimit: asNumber(section.historyLimit, 25, 1, 200),
        historyDir: asString(section.historyDir, `${process.env.HOME ?? ""}/.openclaw/friday-next/history`),
        logLevel: asString(section.logLevel, "info"),
        authToken,
        corsEnabled: asBool(cors.enabled, false),
        corsAllowOrigin: asString(cors.allowOrigin, "*"),
        sseKeepaliveSec: asNumber(sse.keepaliveSec, 30, 5, 120),
        sseBacklogPerDevice: asNumber(sse.backlogPerDevice, 200, 0, 1000),
    };
}
