"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPointsFromAttributes = exports.getAttributeValuesByUri = exports.isSet = exports.isEqualGeoPoints = exports.filterWrongLat = void 0;
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var ramda_1 = require("ramda");
var getAttributeValue = function (attr) {
    var value = attr.label || attr.value || '';
    if (Array.isArray(value)) {
        value = value.join(', ');
    }
    else if ((0, ramda_1.is)(Object, value)) {
        value = '';
    }
    return value;
};
var parseAttributeValues = function (attributes, attrTypeUri) {
    return Object.values(attributes)
        .filter(Array.isArray)
        .flat(Infinity)
        .filter(mdm_sdk_1.isOv)
        .flatMap(function (attribute) {
        var result;
        if (attribute.type == attrTypeUri) {
            result = [getAttributeValue(attribute)];
        }
        if ((0, ramda_1.is)(Object, attribute.value)) {
            result = (result || []).concat(parseAttributeValues(attribute.value, attrTypeUri));
        }
        return result;
    });
};
exports.filterWrongLat = (0, ramda_1.pipe)((0, ramda_1.path)(['point', 'lat']), (0, ramda_1.both)((0, ramda_1.gt)(90), (0, ramda_1.lt)(-90)));
var isEqualGeoPoints = function (aPoint, bPoint) {
    return (aPoint.entity === bPoint.entity &&
        aPoint.point.lat === bPoint.point.lat &&
        aPoint.point.lng === bPoint.point.lng);
};
exports.isEqualGeoPoints = isEqualGeoPoints;
var isSet = function (variable) { return variable && variable.length > 0; };
exports.isSet = isSet;
var getAttributeValuesByUri = function (entity, attrTypeUri) {
    return [].concat((entity === null || entity === void 0 ? void 0 : entity.attributes) ? parseAttributeValues(entity.attributes, attrTypeUri) : []).filter(ramda_1.identity);
};
exports.getAttributeValuesByUri = getAttributeValuesByUri;
var getPointsFromAttributes = function (metadata, entity, geoAttr) {
    if ((0, mdm_sdk_1.hasMaskingByUris)(metadata, [geoAttr.latitude, geoAttr.longitude]))
        return null;
    var lat = (0, exports.getAttributeValuesByUri)(entity, geoAttr.latitude);
    var lng = (0, exports.getAttributeValuesByUri)(entity, geoAttr.longitude);
    if ((0, exports.isSet)(lat) && (0, exports.isSet)(lng)) {
        return (0, ramda_1.zipWith)(function (lat, lng) { return ({ lat: Number(lat), lng: Number(lng) }); }, lat, lng);
    }
    return null;
};
exports.getPointsFromAttributes = getPointsFromAttributes;
