import { resolveAgentIdFromSessionMetadata } from '../resolveAgentIdFromSessionMetadata.js';
import { evaluateVendorResumeEligibility, } from './vendorResumePolicy.js';
function asRecord(value) {
    if (typeof value === 'string') {
        try {
            const parsed = JSON.parse(value);
            if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed))
                return null;
            return parsed;
        }
        catch {
            return null;
        }
    }
    if (!value || typeof value !== 'object' || Array.isArray(value))
        return null;
    return value;
}
function hasConfiguredAcpFlavor(metadata) {
    const flavor = typeof metadata.flavor === 'string' ? metadata.flavor.trim() : '';
    return flavor.toLowerCase().startsWith('acp:') && flavor.slice(4).trim().length > 0;
}
function resolveAgentIdFromMetadata(metadata) {
    const agentId = resolveAgentIdFromSessionMetadata(metadata);
    return agentId && agentId !== 'customAcp' ? agentId : null;
}
export function evaluateExistingSessionAutomationEligibility(input) {
    const metadata = asRecord(input.metadata);
    if (!metadata) {
        return { eligible: false, reasonCode: 'agent_unknown' };
    }
    if (hasConfiguredAcpFlavor(metadata)) {
        return {
            eligible: true,
            agentId: 'customAcp',
            strategy: 'happy_attach',
        };
    }
    const agentId = resolveAgentIdFromMetadata(metadata);
    if (!agentId) {
        return { eligible: false, reasonCode: 'agent_unknown' };
    }
    const eligibility = evaluateVendorResumeEligibility({
        agentId,
        metadata,
        accountSettings: input.accountSettings ?? null,
    });
    if (!eligibility.eligible) {
        return eligibility;
    }
    return {
        eligible: true,
        agentId,
        strategy: 'vendor_resume',
    };
}
//# sourceMappingURL=existingSessionAutomationPolicy.js.map