import moment from 'moment';
import { isNil, uniq } from 'ramda';
import { getParentUri, getOvAttributeValuesByPathWithUri, InputVerificationAttribute, OutputVerificationAttribute, getAttributeTypeUriByValueUri, getEntityUriFromAttributeUri } from '@reltio/mdm-sdk';
export var VerificationStatus;
(function (VerificationStatus) {
    VerificationStatus["VALID"] = "valid";
    VerificationStatus["INVALID"] = "invalid";
    VerificationStatus["OUTDATED"] = "outdated";
    VerificationStatus["MAYBE"] = "maybe";
    VerificationStatus["ACCEPT_ALL"] = "accept_all";
})(VerificationStatus || (VerificationStatus = {}));
var findValueByAttributeUri = function (attributeValue, attributeUri) {
    var _a;
    var entityTypeUri = getEntityUriFromAttributeUri(attributeUri);
    var currentValueUriPath = getAttributeTypeUriByValueUri(attributeValue.uri, entityTypeUri);
    if (!attributeUri.startsWith("".concat(currentValueUriPath, "/attributes/"))) {
        return undefined;
    }
    var attrPath = attributeUri.replace("".concat(currentValueUriPath, "/attributes/"), '').split('/attributes/');
    return ((_a = 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 getParentUri(mapping.attribute) === attributeType.uri; });
    });
};
var findMappingByVerificationAttribute = function (config, verificationAttributes) {
    return config.outputMapping.find(function (mapping) { return verificationAttributes.includes(mapping.verificationAttribute); });
};
export var getIsVerificationAttribute = function (attributeType, attributeVerificationConfig) {
    return attributeVerificationConfig.some(function (config) {
        return config.inputMapping.some(function (mapping) {
            return getParentUri(mapping.attribute) === attributeType.uri &&
                Object.keys(InputVerificationAttribute).includes(mapping.verificationAttribute);
        });
    });
};
var isStatusOutdated = function (dateValue, config) {
    if (isNil(dateValue)) {
        return null;
    }
    var verifDate = moment(dateValue);
    if (!(verifDate === null || verifDate === void 0 ? void 0 : verifDate.isValid())) {
        return null;
    }
    var currentDate = moment();
    var maxVerificationAge = config.params.maxVerificationAge;
    var ageInDays = currentDate.diff(verifDate, 'days');
    return ageInDays > maxVerificationAge;
};
export var getVerificationStatus = function (attributeType, attributeValue, attributeVerificationConfig) {
    var config = findAttributeConfig(attributeVerificationConfig, attributeType);
    if (!config) {
        return null;
    }
    var inputMapping = config.inputMapping.find(function (mapping) { return getParentUri(mapping.attribute) === attributeType.uri; });
    var statusMapping = findMappingByVerificationAttribute(config, [
        OutputVerificationAttribute.Status,
        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, [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 (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
    };
};
export 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;
};
export 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(InputVerificationAttribute).includes(mapping.verificationAttribute); })
            .map(function (mapping) { return getParentUri(mapping.attribute); });
    });
};
export var getAllVerificationTypes = function (attributeVerificationConfig) {
    return uniq(attributeVerificationConfig.flatMap(function (config) {
        return config.inputMapping.map(function (mapping) { return mapping.verificationAttribute; });
    }));
};
