"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.preparePivotingAttributeValue = exports.convertNestedAttributePivotingValue = exports.convertSimpleAttributePivotingValue = exports.generatePivotingValue = void 0;
var mdm_sdk_1 = require("@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 : [];
};
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;
    }, []);
};
exports.generatePivotingValue = generatePivotingValue;
var convertSimpleAttributePivotingValue = function (attrType, value) {
    var _a;
    return [
        (_a = {}, _a[attrType.name] = value, _a)
    ];
};
exports.convertSimpleAttributePivotingValue = convertSimpleAttributePivotingValue;
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 [(0, mdm_sdk_1.getLastUriPart)(key), value];
        }));
    });
};
exports.convertNestedAttributePivotingValue = convertNestedAttributePivotingValue;
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 = (0, mdm_sdk_1.findAttributeTypeByUri)(metadata, valueContainer.type);
            if (value && typeof value !== 'object' && (0, mdm_sdk_1.isOv)(valueContainer)) {
                acc.push((0, exports.preparePivotingAttributeValue)(value, attributeType));
            }
            else if (value && typeof value === 'object' && !Array.isArray(value)) {
                acc.push((0, exports.generatePivotingValue)(attributeType, value, pivotingAttributes, metadata));
            }
        }
        return acc;
    }, []);
};
var preparePivotingAttributeValue = function (value, attributeType) {
    switch (attributeType === null || attributeType === void 0 ? void 0 : attributeType.type) {
        case mdm_sdk_1.DataTypes.TYPE_DATE:
            return new Date(value).getTime();
        case mdm_sdk_1.DataTypes.TYPE_TIMESTAMP: {
            if (!isNaN(Number(value))) {
                return new Date(Number(value)).getTime();
            }
            else {
                return new Date(value).getTime();
            }
        }
        default:
            return value;
    }
};
exports.preparePivotingAttributeValue = preparePivotingAttributeValue;
