"use strict";
/**
 * Stark Law + Anti-Kickback Statute Compliance Pack
 *
 * Stark Law: 42 U.S.C. §1395nn — Physician Self-Referral Prohibition
 * Anti-Kickback Statute: 42 U.S.C. §1320a-7b(b)
 * 2021 Stark/AKS Final Rules: 85 FR 77492 (CMS) / 85 FR 77684 (OIG)
 * 42 CFR §1001.952 — AKS safe harbors
 *
 * Enforces 5 of 7 rules from rules.md:
 *   BLOCK: STARK-001 (FMV benchmark missing), STARK-002 (volume/value compensation),
 *          STARK-003 (written agreement missing), STARK-004 (exception mapping missing)
 *   WARN:  STARK-005 (technology donation safe harbor), STARK-007 (referral pattern anomaly)
 *   SKIP:  STARK-006 — human-attestation (VBC exception analysis requires legal review)
 *
 * @connexum/ai-governance | Kai (CXNI-005) | 2026-05-24
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.STARK_AKS_PACK = exports.STARK_AKS_CATEGORY_CLASSIFIERS = void 0;
exports.STARK_AKS_CATEGORY_CLASSIFIERS = {
    /**
     * STARK-001 — BLOCK
     * Physician compensation arrangement outputs must include FMV benchmark documentation.
     * FMV benchmark source, range low/high, and confirmation that compensation is within range.
     * 42 U.S.C. §1395nn
     *
     * enforcement_pattern: semantic:fmv-benchmark-check
     */
    FMV_BENCHMARK_MISSING: {
        references: ['42 U.S.C. §1395nn — Stark Law FMV requirement for compensation exceptions'],
        classifier: (input) => {
            // Must be a physician compensation arrangement output (JSON field names or natural language)
            // Exclude technology donation arrangements (handled by TECHNOLOGY_DONATION_SAFE_HARBOR)
            const isTechDonationContext = /(emr[_\s]technology[_\s]donation|ehr[_\s]technology[_\s]donation|technology[_\s]donation|emr[_\s]donation)/i.test(input);
            if (isTechDonationContext)
                return false;
            const isCompensationArrangement = /\b(physician\s+(compensation|arrangement|contract|agreement)|compensation\s+arrangement|employment\s+agreement|professional\s+services\s+agreement|PSA)\b|(arrangement[_\s]id|medical[_\s]director[_\s]agreement|proposed[_\s]annual[_\s]compensation|dhs[_\s]referrals|fmv[_\s]documentation)/i.test(input);
            if (!isCompensationArrangement)
                return false;
            // Flag if FMV documentation is explicitly null (JSON: "fmv_documentation":null)
            const fmvDocumentationNull = /(fmv[_\s]documentation["']?\s*:\s*null)/i.test(input);
            if (fmvDocumentationNull)
                return true;
            // Check for FMV documentation (handles JSON field names and natural language)
            const hasFMVSource = /(FMV[_\s](benchmark|source|survey|report|documentation)|fair\s+market\s+value[_\s](source|survey|benchmark|report|data)|independent\s+(appraiser|valuation|FMV)|benchmark[_\s]source|fmv[_\s]documentation\s*[:=]\s*\{)/i.test(input);
            const hasFMVRange = /(FMV[_\s]range|fair\s+market\s+value\s+range|FMV[_\s]range[_\s](low|high|min|max)|compensation\s+within\s+(FMV|fair\s+market\s+value)|within[_\s]fmv[_\s]range)/i.test(input);
            return !hasFMVSource || !hasFMVRange;
        },
    },
    /**
     * STARK-002 — BLOCK
     * Physician compensation must not vary with the volume or value of referrals.
     * 42 U.S.C. §1395nn(a)(2)(B)
     *
     * enforcement_pattern: semantic:volume-value-compensation-check
     */
    VOLUME_VALUE_COMPENSATION_DETECTED: {
        references: ['42 U.S.C. §1395nn(a)(2)(B) — Prohibition on volume/value-based compensation'],
        classifier: (input) => {
            // Must be a compensation structure output (JSON field names or natural language)
            const isCompensation = /\b(compensation\s+(structure|formula|calculation|plan)|bonus\s+(formula|calculation|structure)|physician\s+(bonus|incentive|payment\s+formula))\b|(compensation[_\s]model|wRVU[_\s]components|wRVU[_-]based|total[_\s]compensation[_\s]formula)/i.test(input);
            if (!isCompensation)
                return false;
            // Flag if compensation varies based on referral/DHS volume
            const hasVolumeValueComponent = /(based\s+on\s+(referral|DHS|claim)\s+(volume|count|number)|per\s+(referral|DHS|claim)|referral[_\s](volume|count)\s+(bonus|incentive|payment)|DHS\s+(volume|referrals)\s+(based|driven|tied\s+to)|referred[_\s](imaging|lab)[_\s]ordered)/i.test(input);
            return hasVolumeValueComponent;
        },
    },
    /**
     * STARK-003 — BLOCK
     * Physician compensation arrangements must have a current signed written agreement.
     * Unsigned or expired agreements mean no Stark exception applies.
     *
     * enforcement_pattern: semantic:arrangement-documentation-check
     */
    ARRANGEMENT_DOCUMENTATION_MISSING: {
        references: ['Stark Law written agreement requirements for compensation exceptions'],
        classifier: (input) => {
            // Must be a physician compensation arrangement record
            const isArrangement = /\b(compensation\s+arrangement|employment\s+(arrangement|agreement)|physician\s+contract|professional\s+services\s+agreement|PSA\s+(record|status))\b/i.test(input);
            if (!isArrangement)
                return false;
            // Check for signed agreement status
            const hasSignedAgreement = /(signed[_\s]agreement[_\s]date|agreement\s+(signed|executed|in\s+effect)|signed\s+(contract|agreement|PSA)\s+(dated?|on|effective))/i.test(input);
            // Check for non-expired status
            const hasActiveAgreement = /(agreement[_\s]expiration[_\s]date|expiration[_\s]date[:\s]+20[2-9]\d|valid\s+through|effective\s+through\s+20[2-9]\d)/i.test(input);
            // Flag if either is missing OR if explicitly expired
            const isExpired = /(agreement\s+(expired?|lapsed?)|expiration[_\s]date[:\s]+(202[0-4]|201\d)|expired\s+(on|as\s+of))/i.test(input);
            return !hasSignedAgreement || !hasActiveAgreement || isExpired;
        },
    },
    /**
     * STARK-004 — BLOCK
     * Physician compensation arrangements involving DHS referrals must map to a Stark exception.
     * 42 U.S.C. §1395nn
     *
     * enforcement_pattern: semantic:stark-exception-mapping
     */
    STARK_EXCEPTION_MAPPING_MISSING: {
        references: ['42 U.S.C. §1395nn — Stark Law exception mapping requirement for DHS referrals'],
        classifier: (input) => {
            // Must involve DHS referrals
            const hasDHSReferrals = /\b(DHS\s+referral(s?)|designated\s+health\s+service\s+referral|refers?\s+(DHS|designated\s+health\s+service)|DHS\s+volume)\b/i.test(input);
            if (!hasDHSReferrals)
                return false;
            // Check for Stark exception mapping.
            // Broadened (PR-2, 2026-05-24): natural language places the exception type BEFORE or AFTER
            // the word "exception". Original pattern only matched "exception (type)" (type after).
            // Now accepts both orders:
            //   "exception employment" / "exception personal services"  (original)
            //   "employment exception" / "personal services exception"  (reversed — common in legal prose)
            // The trailing qualifier group (applies/documented/etc.) is fully optional including its
            // leading whitespace, so bare "employment exception." (sentence end) also matches.
            // Before broadening: 1/5 natural-language fire rate. After: ≥4/5.
            const hasExceptionMapping = /(applicable[_\s]stark[_\s]exception|stark[_\s]exception\s*([:=]|mapped?|applies?|documented?)|exception\s+(employment|personal\s+services|fair\s+market\s+value|indirect\s+compensation|value.?based)|(employment|personal\s+services|fair\s+market\s+value|indirect\s+compensation|value.?based)\s+exception(?:\s+(applies?|documented?|mapped?|confirmed?|identified?))?)/i.test(input);
            return !hasExceptionMapping;
        },
    },
    /**
     * STARK-005 — WARN
     * Technology donation arrangements must satisfy AKS safe harbor requirements.
     * EMR safe harbor: recipient contributes >=15% of donor cost, not conditioned on referrals.
     * 42 CFR §1001.952(x) (EMR) and §1001.952(jj) (cybersecurity)
     *
     * enforcement_pattern: semantic:technology-donation-safe-harbor
     */
    TECHNOLOGY_DONATION_SAFE_HARBOR: {
        references: ['42 CFR §1001.952(x) — EMR safe harbor; §1001.952(jj) — cybersecurity safe harbor'],
        classifier: (input) => {
            // Must be a technology donation arrangement (JSON field names or natural language)
            const isTechDonation = /\b(technology\s+donation|EMR\s+donation|EHR\s+donation|electronic\s+(health|medical)\s+records?\s+donation|cybersecurity\s+donation|software\s+donation)\b|(emr[_\s]technology[_\s]donation|ehr[_\s]technology[_\s]donation|arrangement[_\s]type["']?\s*:\s*["']?(emr|ehr)[_\s]technology[_\s]donation)/i.test(input);
            if (!isTechDonation)
                return false;
            // Check recipient contribution percentage
            const hasContributionCheck = /(recipient[_\s]contribution[_\s]percentage|recipient\s+(contributes?|pays?)\s+(\d+)%|15%\s+(contribution|requirement)|contribution[_\s]percentage\s*[:=]\s*\d+|recipient[_\s]contribution[_\s]at[_\s]least[_\s]15)/i.test(input);
            const hasReferralConditionCheck = /(not\s+conditioned\s+on\s+referral(s?)|referral[_\s]conditionality[_\s]check|not\s+tied\s+to\s+referral(s?)|referral[_\s]conditionality[_\s]attestation|referral[_\s]conditionality["']?\s*:\s*(false|"false"))/i.test(input);
            return !hasContributionCheck || !hasReferralConditionCheck;
        },
    },
    /**
     * STARK-007 — WARN
     * Referral pattern monitoring: flag statistically anomalous physician referral patterns.
     * Per OIG enforcement guidance and Stark compliance best practices.
     *
     * enforcement_pattern: semantic:referral-pattern-anomaly
     */
    REFERRAL_PATTERN_ANOMALY: {
        references: ['OIG enforcement guidance — undisclosed financial relationships and referral patterns'],
        classifier: (input) => {
            // Must be a referral pattern analysis output
            const isReferralAnalysis = /\b(referral\s+pattern\s+(analysis|monitoring|report|anomaly)|physician\s+referral\s+(pattern|data|analysis)|DHS\s+referral\s+(pattern|concentration|analysis))\b/i.test(input);
            if (!isReferralAnalysis)
                return false;
            // Flag if anomalous concentration is detected
            const hasAnomalyFlag = /(anomalous|statistically\s+anomalous|above\s+(peer|average|benchmark)|standard\s+deviations?\s+above|concentration\s+(above|exceeds?|high)|outlier\s+(physician|referral))/i.test(input);
            const hasMissingArrangement = /(no\s+(documented|known|on.file)\s+arrangement|arrangement\s+not\s+(found|on\s+file|documented)|undisclosed\s+(arrangement|relationship))/i.test(input);
            return hasAnomalyFlag && hasMissingArrangement;
        },
    },
};
exports.STARK_AKS_PACK = {
    categoryClassifiers: exports.STARK_AKS_CATEGORY_CLASSIFIERS,
    id: 'stark-aks',
    name: 'Stark Law + Anti-Kickback Statute',
    version: '1.0.0',
    contentHash: 'd71d235e4c32884afec815ebe361bb7230c7ea5452b66c474058dcc71124a7e7',
    description: 'Physician self-referral (Stark Law, 42 U.S.C. §1395nn) and Anti-Kickback Statute (42 U.S.C. §1320a-7b(b)) compliance for AI systems managing physician compensation arrangements, contract management, referral monitoring, and value-based care programs.',
    authority: 'CMS (Stark Law) / OIG (Anti-Kickback Statute)',
    industries: ['healthcare', 'hospital', 'physician-practice', 'health-system', 'compliance'],
    requiredModules: [
        {
            name: 'audit-integrity',
            required: true,
            reason: 'Compensation arrangement audit trail for OIG investigations',
            reference: '42 U.S.C. §1395nn; 42 U.S.C. §1320a-7b(b)',
        },
        {
            name: 'governance-runtime',
            required: true,
            reason: 'Block arrangement outputs missing FMV documentation or exception mapping',
            reference: '42 U.S.C. §1395nn',
        },
        {
            name: 'approval-queue',
            required: true,
            reason: 'Route flagged arrangements to legal for exception analysis',
            reference: '42 U.S.C. §1395nn — Stark exception analysis',
        },
    ],
    moduleConfig: {
        'audit-integrity': {
            retentionDays: 2555, // 7 years per Stark record retention guidance
        },
        'governance-runtime': {
            denyByDefaultRequired: true,
        },
    },
    providerPolicy: {
        blockedProviders: [],
        requiredCapabilities: [],
        blockReasons: {},
    },
    dataPolicy: {
        scanCategories: [
            'FMV_BENCHMARK_MISSING',
            'VOLUME_VALUE_COMPENSATION_DETECTED',
            'ARRANGEMENT_DOCUMENTATION_MISSING',
            'STARK_EXCEPTION_MAPPING_MISSING',
            'TECHNOLOGY_DONATION_SAFE_HARBOR',
            'REFERRAL_PATTERN_ANOMALY',
        ],
        categoryActions: {
            FMV_BENCHMARK_MISSING: 'BLOCK',
            VOLUME_VALUE_COMPENSATION_DETECTED: 'BLOCK',
            ARRANGEMENT_DOCUMENTATION_MISSING: 'BLOCK',
            STARK_EXCEPTION_MAPPING_MISSING: 'BLOCK',
            TECHNOLOGY_DONATION_SAFE_HARBOR: 'WARN',
            REFERRAL_PATTERN_ANOMALY: 'WARN',
        },
        encryptionRequired: true,
        encryptionStandard: 'AES-256-GCM',
        minimumClassification: 'RESTRICTED',
    },
    retentionPolicy: {
        auditLogRetentionDays: 2555,
        sessionRetentionDays: 2555,
        incidentRetentionDays: 2555,
        reference: 'Stark Law record retention — 7 years per OIG guidance',
        forceDelete: false,
        requiresLegalHoldSupport: true,
        sovereignty: ['us'],
    },
    incidentPolicy: {
        notificationDeadlineHours: 24,
        reference: '42 U.S.C. §1395nn; OIG self-disclosure protocol',
        requiredRecipients: ['compliance-officer', 'legal-counsel', 'cmo'],
        formalNotificationRequired: true,
    },
    validators: [
        {
            id: 'stark-fmv-check',
            description: 'FMV benchmark documentation classifier active',
            reference: '42 U.S.C. §1395nn — Stark Law FMV requirement',
            check: 'data_classifier_active',
            failSeverity: 'CRITICAL',
        },
        {
            id: 'stark-exception-mapping',
            description: 'Stark exception mapping classifier active',
            reference: '42 U.S.C. §1395nn — DHS referral arrangement exception mapping',
            check: 'data_classifier_active',
            failSeverity: 'CRITICAL',
        },
    ],
    requiresRetrievalAuditLogging: true,
    maxAutonomyLevelWithoutAttestation: 2,
    recommendedHookProfile: 'strict',
};
//# sourceMappingURL=stark-aks.js.map