// Copyright 2024-2026 Tymofii Pidlisnyi. Apache-2.0 license. See LICENSE.
/**
 * APS v2 Policy Profiles (Section 9.14)
 *
 * Jurisdiction-tagged, domain-tagged compliance modules. The core protocol
 * governs identity, delegation, provenance. Policy Profiles govern child
 * safety, fraud prevention, IP, speech, finance, healthcare — everything
 * politically contingent or sector-specific.
 */
const profiles = new Map();
const attachments = [];
export function createProfile(params) {
    const p = {
        id: `profile-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`,
        name: params.name, jurisdiction: params.jurisdiction,
        domain: params.domain, version: params.version,
        constraints: params.constraints,
        created_at: new Date().toISOString(),
    };
    profiles.set(p.id, p);
    return p;
}
export function attachProfile(profileId, targetType, targetId, attachedBy) {
    if (!profiles.has(profileId))
        throw new Error(`Profile ${profileId} not found`);
    const a = {
        id: `attach-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`,
        profile_id: profileId, target_type: targetType,
        target_id: targetId, attached_at: new Date().toISOString(),
        attached_by: attachedBy,
    };
    attachments.push(a);
    return a;
}
export function getProfilesForTarget(targetType, targetId) {
    const attached = attachments.filter(a => a.target_type === targetType && a.target_id === targetId);
    return attached.map(a => profiles.get(a.profile_id)).filter(Boolean);
}
export function checkProfileCompliance(targetType, targetId, actionMetadata) {
    const targetProfiles = getProfilesForTarget(targetType, targetId);
    const violations = [];
    for (const profile of targetProfiles) {
        for (const c of profile.constraints) {
            if (c.type === 'prohibited_action') {
                const actionType = actionMetadata['action_type'] || '';
                const prohibited = String(c.parameters['action'] || '');
                if (actionType === prohibited) {
                    violations.push(`[${profile.name}] Prohibited action: ${prohibited}`);
                }
            }
            if (c.type === 'mandatory_human_signoff') {
                const hasSignoff = actionMetadata['human_signoff'] === 'true';
                if (!hasSignoff) {
                    violations.push(`[${profile.name}] Requires human signoff: ${c.description}`);
                }
            }
            if (c.type === 'content_restriction') {
                const content = actionMetadata['content'] || '';
                const restricted = String(c.parameters['keyword'] || '');
                if (restricted && content.toLowerCase().includes(restricted.toLowerCase())) {
                    violations.push(`[${profile.name}] Content restriction: ${c.description}`);
                }
            }
        }
    }
    return { compliant: violations.length === 0, violations };
}
export function detachProfile(attachmentId) {
    const idx = attachments.findIndex(a => a.id === attachmentId);
    if (idx >= 0)
        attachments.splice(idx, 1);
}
export function listActiveProfiles() { return [...profiles.values()]; }
export function getProfile(id) { return profiles.get(id); }
export function clearPolicyProfileStores() { profiles.clear(); attachments.length = 0; }
//# sourceMappingURL=policy-profiles.js.map