/**
 * DevExtreme (esm/__internal/core/license/license_validation.js)
 * Version: 23.2.6 (build 24140-0112)
 * Build date: Sun May 19 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
import _extends from "@babel/runtime/helpers/esm/extends";
var __rest = this && this.__rest || function(s, e) {
    var t = {};
    for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) {
            t[p] = s[p]
        }
    }
    if (null != s && "function" === typeof Object.getOwnPropertySymbols) {
        var i = 0;
        for (p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) {
                t[p[i]] = s[p[i]]
            }
        }
    }
    return t
};
import errors from "../../../core/errors";
import {
    version as packageVersion
} from "../../../core/version";
import {
    base64ToBytes
} from "./byte_utils";
import {
    INTERNAL_USAGE_ID,
    PUBLIC_KEY
} from "./key";
import {
    pad
} from "./pkcs1";
import {
    compareSignatures
} from "./rsa_bigint";
import {
    sha1
} from "./sha1";
import {
    TokenKind
} from "./types";
var SPLITTER = ".";
var FORMAT = 1;
var RTM_MIN_PATCH_VERSION = 3;
var GENERAL_ERROR = {
    kind: TokenKind.corrupted,
    error: "general"
};
var VERIFICATION_ERROR = {
    kind: TokenKind.corrupted,
    error: "verification"
};
var DECODING_ERROR = {
    kind: TokenKind.corrupted,
    error: "decoding"
};
var DESERIALIZATION_ERROR = {
    kind: TokenKind.corrupted,
    error: "deserialization"
};
var PAYLOAD_ERROR = {
    kind: TokenKind.corrupted,
    error: "payload"
};
var VERSION_ERROR = {
    kind: TokenKind.corrupted,
    error: "version"
};
var validationPerformed = false;

function verifySignature(_ref) {
    var {
        text: text,
        signature: encodedSignature
    } = _ref;
    return compareSignatures({
        key: PUBLIC_KEY,
        signature: base64ToBytes(encodedSignature),
        actual: pad(sha1(text))
    })
}
export function parseLicenseKey(encodedKey) {
    if (void 0 === encodedKey) {
        return GENERAL_ERROR
    }
    var parts = encodedKey.split(SPLITTER);
    if (2 !== parts.length || 0 === parts[0].length || 0 === parts[1].length) {
        return GENERAL_ERROR
    }
    if (!verifySignature({
            text: parts[0],
            signature: parts[1]
        })) {
        return VERIFICATION_ERROR
    }
    var decodedPayload = "";
    try {
        decodedPayload = atob(parts[0])
    } catch (_a) {
        return DECODING_ERROR
    }
    var payload = {};
    try {
        payload = JSON.parse(decodedPayload)
    } catch (_b) {
        return DESERIALIZATION_ERROR
    }
    var {
        customerId: customerId,
        maxVersionAllowed: maxVersionAllowed,
        format: format,
        internalUsageId: internalUsageId
    } = payload, rest = __rest(payload, ["customerId", "maxVersionAllowed", "format", "internalUsageId"]);
    if (void 0 !== internalUsageId) {
        return {
            kind: TokenKind.internal,
            internalUsageId: internalUsageId
        }
    }
    if (void 0 === customerId || void 0 === maxVersionAllowed || void 0 === format) {
        return PAYLOAD_ERROR
    }
    if (format !== FORMAT) {
        return VERSION_ERROR
    }
    return {
        kind: TokenKind.verified,
        payload: _extends({
            customerId: customerId,
            maxVersionAllowed: maxVersionAllowed
        }, rest)
    }
}

function getLicenseCheckParams(_ref2) {
    var {
        licenseKey: licenseKey,
        version: version
    } = _ref2;
    var preview = false;
    try {
        var [major, minor, patch] = version.split(".").map(Number);
        preview = isNaN(patch) || patch < RTM_MIN_PATCH_VERSION;
        if (!licenseKey) {
            return {
                preview: preview,
                error: "W0019"
            }
        }
        var license = parseLicenseKey(licenseKey);
        if (license.kind === TokenKind.corrupted) {
            return {
                preview: preview,
                error: "W0021"
            }
        }
        if (license.kind === TokenKind.internal) {
            return {
                preview: preview,
                internal: true,
                error: license.internalUsageId === INTERNAL_USAGE_ID ? void 0 : "W0020"
            }
        }
        if (!(major && minor)) {
            return {
                preview: preview,
                error: "W0021"
            }
        }
        if (10 * major + minor > license.payload.maxVersionAllowed) {
            return {
                preview: preview,
                error: "W0020"
            }
        }
        return {
            preview: preview,
            error: void 0
        }
    } catch (_a) {
        return {
            preview: preview,
            error: "W0021"
        }
    }
}
export function validateLicense(licenseKey) {
    var version = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : packageVersion;
    if (validationPerformed) {
        return
    }
    validationPerformed = true;
    var {
        preview: preview,
        internal: internal,
        error: error
    } = getLicenseCheckParams({
        licenseKey: licenseKey,
        version: version
    });
    if (error) {
        errors.log(preview ? "W0022" : error);
        return
    }
    if (preview && !internal) {
        errors.log("W0022")
    }
}
export function peekValidationPerformed() {
    return validationPerformed
}
export function setLicenseCheckSkipCondition() {}
export default {
    validateLicense: validateLicense
};
