import { DataTypes, findAttributeTypeByUri, isOv, getLastUriPart } from '@reltio/mdm-sdk';
var getPivotingChildrenUris = function (attributeType, pivotingAttributes) {
    var pivotingAttribute = pivotingAttributes.find(function (attribute) {
        return attribute.uri === attributeType.uri;
    });
    return pivotingAttribute && Array.isArray(pivotingAttribute.children) ? pivotingAttribute.children : [];
};
export var generatePivotingValue = function (attributeType, value, pivotingAttributes, metadata) {
    var pivotingChildrenUris = getPivotingChildrenUris(attributeType, pivotingAttributes);
    var pivotingChildren = (attributeType === null || attributeType === void 0 ? void 0 : attributeType.attributes) || (attributeType === null || attributeType === void 0 ? void 0 : attributeType.analyticsAttributes) || [];
    if (pivotingChildrenUris.length) {
        pivotingChildren = pivotingChildren.filter(function (attrType) { return pivotingChildrenUris.includes(attrType.uri); });
    }
    return pivotingChildren.reduce(function (acc, attribute) {
        var _a, _b;
        var values = valueToString(value[attribute.name], metadata, pivotingAttributes);
        if (values && values.length) {
            if (Array.isArray(values[0])) {
                acc.push((_a = {}, _a[attribute.uri] = values[0], _a));
            }
            else {
                acc.push((_b = {}, _b[attribute.uri] = values, _b));
            }
        }
        return acc;
    }, []);
};
export var convertSimpleAttributePivotingValue = function (attrType, value) {
    var _a;
    return [
        (_a = {}, _a[attrType.name] = value, _a)
    ];
};
export var convertNestedAttributePivotingValue = function (values) {
    return values.map(function (value) {
        return Object.fromEntries(Object.entries(value).map(function (_a) {
            var key = _a[0], value = _a[1];
            return [getLastUriPart(key), value];
        }));
    });
};
var valueToString = function (values, metadata, pivotingAttributes) {
    if (!values || values.length === 0)
        return;
    return values.reduce(function (acc, valueContainer) {
        if ('type' in valueContainer && valueContainer.type) {
            var value = 'lookupCode' in valueContainer ? valueContainer.lookupCode : valueContainer.value;
            var attributeType = findAttributeTypeByUri(metadata, valueContainer.type);
            if (value && typeof value !== 'object' && isOv(valueContainer)) {
                acc.push(preparePivotingAttributeValue(value, attributeType));
            }
            else if (value && typeof value === 'object' && !Array.isArray(value)) {
                acc.push(generatePivotingValue(attributeType, value, pivotingAttributes, metadata));
            }
        }
        return acc;
    }, []);
};
export var preparePivotingAttributeValue = function (value, attributeType) {
    switch (attributeType === null || attributeType === void 0 ? void 0 : attributeType.type) {
        case DataTypes.TYPE_DATE:
            return new Date(value).getTime();
        case DataTypes.TYPE_TIMESTAMP: {
            if (!isNaN(Number(value))) {
                return new Date(Number(value)).getTime();
            }
            else {
                return new Date(value).getTime();
            }
        }
        default:
            return value;
    }
};
