// ---------------------------------------------------------------------------
// License compatibility map
// ---------------------------------------------------------------------------
const LICENSE_COMPAT = {
    MIT: 'compatible',
    'Apache-2.0': 'compatible',
    'BSD-2-Clause': 'compatible',
    'BSD-3-Clause': 'compatible',
    ISC: 'compatible',
    '0BSD': 'compatible',
    Unlicense: 'compatible',
    CC0: 'compatible',
    'CC0-1.0': 'compatible',
    WTFPL: 'compatible',
    'Artistic-2.0': 'compatible',
    Zlib: 'compatible',
    GPL: 'critical',
    'GPL-2.0': 'critical',
    'GPL-2.0-only': 'critical',
    'GPL-2.0-or-later': 'critical',
    'GPL-3.0': 'critical',
    'GPL-3.0-only': 'critical',
    'GPL-3.0-or-later': 'critical',
    AGPL: 'critical',
    'AGPL-3.0': 'critical',
    'AGPL-3.0-only': 'critical',
    'AGPL-3.0-or-later': 'critical',
    LGPL: 'warning',
    'LGPL-2.0': 'warning',
    'LGPL-2.1': 'warning',
    'LGPL-2.1-only': 'warning',
    'LGPL-2.1-or-later': 'warning',
    'LGPL-3.0': 'warning',
    'LGPL-3.0-only': 'warning',
    'LGPL-3.0-or-later': 'warning',
    MPL: 'warning',
    'MPL-2.0': 'warning',
    CDDL: 'warning',
    'CDDL-1.0': 'warning',
    EPL: 'warning',
    'EPL-1.0': 'warning',
    'EPL-2.0': 'warning',
    EUPL: 'warning',
    'EUPL-1.2': 'warning',
};
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function normalizeIdentifier(raw) {
    // Some packages use "(MIT OR Apache-2.0)" — extract first token
    const stripped = raw.replace(/[()]/g, '').trim();
    if (stripped.includes(' OR ')) {
        const first = stripped.split(' OR ')[0]?.trim() ?? stripped;
        return first;
    }
    return stripped;
}
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/**
 * Determine compatibility of a license identifier.
 * GPL/AGPL → critical (violates commercial use).
 * LGPL/MPL/CDDL/EPL → warning (requires care).
 * MIT/Apache/BSD/ISC/etc. → compatible.
 * Anything else → unknown.
 */
export function checkLicense(license) {
    if (!license || license.trim() === '') {
        return {
            license: '',
            compatibility: 'unknown',
            reason: 'No license declared — treat as proprietary; manual review required.',
        };
    }
    const normalized = normalizeIdentifier(license);
    const compat = LICENSE_COMPAT[normalized];
    if (compat === 'compatible') {
        return {
            license,
            compatibility: 'compatible',
            reason: `${license} is permissive and compatible with commercial use.`,
        };
    }
    if (compat === 'critical') {
        return {
            license,
            compatibility: 'critical',
            reason: `${license} is a copyleft license. Using it in a commercial project requires releasing source under the same terms.`,
        };
    }
    if (compat === 'warning') {
        return {
            license,
            compatibility: 'warning',
            reason: `${license} has weak copyleft conditions. Modifications to the library itself must be shared, but linking is usually allowed.`,
        };
    }
    return {
        license,
        compatibility: 'unknown',
        reason: `License "${license}" is not recognized. Manual review required.`,
    };
}
/**
 * Determine if the project is commercial (default assumption when no license field).
 * Reads the package.json content string and inspects the `license` field.
 * Non-commercial licenses: GPL, AGPL, CC-BY-NC, etc.
 */
export function isCommercialProject(packageJsonContent) {
    let parsed;
    try {
        parsed = JSON.parse(packageJsonContent);
    }
    catch {
        return true; // assume commercial on parse error
    }
    if (typeof parsed !== 'object' || parsed === null) {
        return true;
    }
    const obj = parsed;
    const license = typeof obj.license === 'string' ? obj.license : '';
    if (!license) {
        return true; // no license field → assume commercial
    }
    const lowerLicense = license.toLowerCase();
    const nonCommercialKeywords = ['gpl', 'agpl', 'cc-by-nc', 'noncommercial', 'non-commercial'];
    const isNonCommercial = nonCommercialKeywords.some((kw) => lowerLicense.includes(kw));
    return !isNonCommercial;
}
//# sourceMappingURL=license-checker.js.map