import { isEqualWith } from 'lodash-es';
export const getLowestLevelObject = (obj) => {
    var _a;
    if (!!!(obj === null || obj === void 0 ? void 0 : obj.options) || ((_a = obj === null || obj === void 0 ? void 0 : obj.options) === null || _a === void 0 ? void 0 : _a.length) === 0) {
        return obj;
    }
    else {
        return getLowestLevelObject(obj.options[0]);
    }
};
export const getChildrenObjectsWithNoChildren = (arr, valueKey) => {
    if (!!!arr)
        return [];
    return arr.reduce((result, obj) => {
        var _a;
        const children = (_a = obj === null || obj === void 0 ? void 0 : obj.options) !== null && _a !== void 0 ? _a : [];
        const childrenWithNoChildren = getChildrenObjectsWithNoChildren(children, valueKey);
        if (childrenWithNoChildren.length > 0) {
            result.push(...childrenWithNoChildren);
        }
        else if (children.length === 0) {
            result.push(obj[valueKey]);
        }
        return result;
    }, []);
};
export const searchForLayerOptions = (data, searchString = '') => {
    var _a, _b;
    let result = [];
    for (let i = 0; i < (data === null || data === void 0 ? void 0 : data.length); i++) {
        const obj = data[i];
        const { title, value, options } = obj;
        const titleString = typeof title !== 'string' ? title.toString() : title;
        const valueString = typeof value !== 'string' ? value.toString() : value;
        if (((_a = titleString === null || titleString === void 0 ? void 0 : titleString.toLowerCase()) === null || _a === void 0 ? void 0 : _a.includes(searchString === null || searchString === void 0 ? void 0 : searchString.toLowerCase())) ||
            ((_b = valueString === null || valueString === void 0 ? void 0 : valueString.toLowerCase()) === null || _b === void 0 ? void 0 : _b.includes(searchString === null || searchString === void 0 ? void 0 : searchString.toLowerCase()))) {
            result.push(obj);
        }
        else if (options && (options === null || options === void 0 ? void 0 : options.length) > 0) {
            const childOptions = searchForLayerOptions(options, searchString);
            if ((childOptions === null || childOptions === void 0 ? void 0 : childOptions.length) > 0) {
                result.push({ title, value, options: childOptions });
            }
        }
    }
    return result;
};
export const getLayer = (layerKeys, layerKeySeparator, obj) => {
    let stringValue = '';
    layerKeys === null || layerKeys === void 0 ? void 0 : layerKeys.forEach((key, index) => {
        stringValue +=
            obj[key] + (index !== (layerKeys === null || layerKeys === void 0 ? void 0 : layerKeys.length) - 1 ? layerKeySeparator : '');
    });
    return stringValue;
};
export const generateGeoJsonList = (data, mappingConfig) => {
    if ((data === null || data === void 0 ? void 0 : data.length) > 0 && mappingConfig) {
        return data === null || data === void 0 ? void 0 : data.map((item) => {
            return {
                type: 'Feature',
                id: item[mappingConfig.featureId],
                geometry: {
                    type: item[mappingConfig.geometryType],
                    coordinates: item[mappingConfig.coordinates]
                },
                properties: mappingConfig.propertiesObj && !!item[mappingConfig.propertiesObj]
                    ? Object.assign(Object.assign({}, item[mappingConfig.propertiesObj]), { id: item[mappingConfig.featureId] }) : Object.assign(Object.assign({}, item), { id: item[mappingConfig.featureId] })
            };
        });
    }
    return [];
};
export function getKeyValuePairs(object) {
    return Object.keys(object).map((key) => {
        return { key: key, value: object[key] };
    });
}
export function filterObjectKeys(object, keysToKeep, removeObjects = false) {
    if (removeObjects) {
        return Object.entries(object)
            .filter(([key, value]) => typeof value !== 'object' && keysToKeep.includes(key))
            .reduce((acc, [key, value]) => {
            acc[key] = value ? value : '-';
            return acc;
        }, {});
    }
    return Object.entries(object)
        .filter(([key]) => keysToKeep.includes(key))
        .reduce((acc, [key, value]) => {
        acc[key] = value ? value : '-';
        return acc;
    }, {});
}
export function getSelectedFeaturePropertyKeyValuePairs(featurePropertiesToDisplay = [], featureProperties) {
    let keyVals = [];
    if (featureProperties) {
        if (featurePropertiesToDisplay) {
            const filteredObj = filterObjectKeys(featureProperties, featurePropertiesToDisplay, true);
            keyVals = filteredObj ? getKeyValuePairs(filteredObj) : [];
        }
        else {
            keyVals = featureProperties ? getKeyValuePairs(featureProperties) : [];
        }
    }
    return keyVals;
}
export const isEqualIgnoreFunctions = (a, b) => isEqualWith(a, b, (val1, val2) => {
    if (typeof val1 === 'function' && typeof val2 === 'function') {
        return true; // ignore function differences
    }
    // fall back to default comparison
    return undefined;
});
//# sourceMappingURL=MapUtilities.js.map