"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAllVerificationTypes = exports.getAllVerificationAttributeTypeUris = exports.getVerificationType = exports.getVerificationStatus = exports.getIsVerificationAttribute = exports.VerificationStatus = void 0;
var moment_1 = __importDefault(require("moment"));
var ramda_1 = require("ramda");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var VerificationStatus;
(function (VerificationStatus) {
    VerificationStatus["VALID"] = "valid";
    VerificationStatus["INVALID"] = "invalid";
    VerificationStatus["OUTDATED"] = "outdated";
    VerificationStatus["MAYBE"] = "maybe";
    VerificationStatus["ACCEPT_ALL"] = "accept_all";
})(VerificationStatus || (exports.VerificationStatus = VerificationStatus = {}));
var findValueByAttributeUri = function (attributeValue, attributeUri) {
    var _a;
    var entityTypeUri = (0, mdm_sdk_1.getEntityUriFromAttributeUri)(attributeUri);
    var currentValueUriPath = (0, mdm_sdk_1.getAttributeTypeUriByValueUri)(attributeValue.uri, entityTypeUri);
    if (!attributeUri.startsWith("".concat(currentValueUriPath, "/attributes/"))) {
        return undefined;
    }
    var attrPath = attributeUri.replace("".concat(currentValueUriPath, "/attributes/"), '').split('/attributes/');
    return ((_a = (0, mdm_sdk_1.getOvAttributeValuesByPathWithUri)(attrPath, attributeValue.value)) === null || _a === void 0 ? void 0 : _a[0]) || undefined;
};
var findAttributeConfig = function (attributeVerificationConfig, attributeType) {
    return attributeVerificationConfig.find(function (config) {
        return config.inputMapping.some(function (mapping) { return (0, mdm_sdk_1.getParentUri)(mapping.attribute) === attributeType.uri; });
    });
};
var findMappingByVerificationAttribute = function (config, verificationAttributes) {
    return config.outputMapping.find(function (mapping) { return verificationAttributes.includes(mapping.verificationAttribute); });
};
var getIsVerificationAttribute = function (attributeType, attributeVerificationConfig) {
    return attributeVerificationConfig.some(function (config) {
        return config.inputMapping.some(function (mapping) {
            return (0, mdm_sdk_1.getParentUri)(mapping.attribute) === attributeType.uri &&
                Object.keys(mdm_sdk_1.InputVerificationAttribute).includes(mapping.verificationAttribute);
        });
    });
};
exports.getIsVerificationAttribute = getIsVerificationAttribute;
var isStatusOutdated = function (dateValue, config) {
    if ((0, ramda_1.isNil)(dateValue)) {
        return null;
    }
    var verifDate = (0, moment_1.default)(dateValue);
    if (!(verifDate === null || verifDate === void 0 ? void 0 : verifDate.isValid())) {
        return null;
    }
    var currentDate = (0, moment_1.default)();
    var maxVerificationAge = config.params.maxVerificationAge;
    var ageInDays = currentDate.diff(verifDate, 'days');
    return ageInDays > maxVerificationAge;
};
var getVerificationStatus = function (attributeType, attributeValue, attributeVerificationConfig) {
    var config = findAttributeConfig(attributeVerificationConfig, attributeType);
    if (!config) {
        return null;
    }
    var inputMapping = config.inputMapping.find(function (mapping) { return (0, mdm_sdk_1.getParentUri)(mapping.attribute) === attributeType.uri; });
    var statusMapping = findMappingByVerificationAttribute(config, [
        mdm_sdk_1.OutputVerificationAttribute.Status,
        mdm_sdk_1.OutputVerificationAttribute.IsValid
    ]);
    if (!statusMapping) {
        return null;
    }
    var statusValue = findValueByAttributeUri(attributeValue, statusMapping.attribute);
    var valueStatus = statusValue === null || statusValue === void 0 ? void 0 : statusValue.value;
    var dateMapping = findMappingByVerificationAttribute(config, [mdm_sdk_1.OutputVerificationAttribute.VerifiedOn]);
    if (!dateMapping) {
        return null;
    }
    var dateValue = findValueByAttributeUri(attributeValue, dateMapping.attribute);
    if (!dateValue || !statusValue) {
        return null;
    }
    var verificationStatus = (function () {
        switch (valueStatus) {
            case 'Invalid':
            case 'No':
                return VerificationStatus.INVALID;
            case 'Valid':
            case 'Yes': {
                var isStatusOutdatedResult = isStatusOutdated(dateValue === null || dateValue === void 0 ? void 0 : dateValue.value, config);
                if ((0, ramda_1.isNil)(isStatusOutdatedResult)) {
                    return null;
                }
                if (isStatusOutdatedResult) {
                    return VerificationStatus.OUTDATED;
                }
                return VerificationStatus.VALID;
            }
            case 'Maybe':
            case 'Unknown':
                return VerificationStatus.MAYBE;
            case 'Accept_All':
                return VerificationStatus.ACCEPT_ALL;
            default: {
                return null;
            }
        }
    })();
    if (verificationStatus === null) {
        return null;
    }
    return {
        status: verificationStatus,
        lastVerifiedTime: dateValue === null || dateValue === void 0 ? void 0 : dateValue.value,
        verificationType: inputMapping.verificationAttribute
    };
};
exports.getVerificationStatus = getVerificationStatus;
var getVerificationType = function (attributeType, attributeVerificationConfig) {
    var _a, _b;
    var config = findAttributeConfig(attributeVerificationConfig, attributeType);
    return ((_b = (_a = config === null || config === void 0 ? void 0 : config.inputMapping) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.verificationAttribute) || null;
};
exports.getVerificationType = getVerificationType;
var getAllVerificationAttributeTypeUris = function (attributeVerificationConfig, entityTypeUri) {
    return attributeVerificationConfig.flatMap(function (config) {
        return config.inputMapping
            .filter(function (mapping) { return mapping.attribute.startsWith(entityTypeUri + '/'); })
            .filter(function (mapping) { return Object.keys(mdm_sdk_1.InputVerificationAttribute).includes(mapping.verificationAttribute); })
            .map(function (mapping) { return (0, mdm_sdk_1.getParentUri)(mapping.attribute); });
    });
};
exports.getAllVerificationAttributeTypeUris = getAllVerificationAttributeTypeUris;
var getAllVerificationTypes = function (attributeVerificationConfig) {
    return (0, ramda_1.uniq)(attributeVerificationConfig.flatMap(function (config) {
        return config.inputMapping.map(function (mapping) { return mapping.verificationAttribute; });
    }));
};
exports.getAllVerificationTypes = getAllVerificationTypes;
