import { __values } from "tslib";
import { IGXE } from "@intelligentgraphics/igd.core";
/**
 * Finds all articles where a specific value for a property could be set
 *
 * @param {Article[]} objects
 * @param {string} propertyKey
 * @param {IGXE.TPropertyValue} propertyValue
 * @returns {string[]} comIds
 */
export var findApplyTargets = function (objects, propertyKey, propertyValue) {
    var e_1, _a;
    var targetable = [];
    var normalizedValue = propertyValue.toString();
    var rawValuePath = IGXE.getPropertyNamesFromKey(normalizedValue);
    var valueParentProperty = rawValuePath.length <= 1
        ? propertyKey
        : rawValuePath[rawValuePath.length - 2];
    var rootPropertyKey = IGXE.getPropertyNamesFromKey(propertyKey)[0];
    var valueEqualsNormalized = function (_a) {
        var Value = _a.Value;
        return Value.toString() === normalizedValue;
    };
    try {
        for (var objects_1 = __values(objects), objects_1_1 = objects_1.next(); !objects_1_1.done; objects_1_1 = objects_1.next()) {
            var articleInstance = objects_1_1.value;
            if (articleInstance.configuration[rootPropertyKey] === propertyValue) {
                continue;
            }
            var propertyData = IGXE.findProperty(articleInstance.properties, valueParentProperty);
            if (propertyData === undefined) {
                continue;
            }
            if (normalizedValue.match(/^[\d\.]+$/) &&
                propertyData.ValueRangeFrom !== undefined &&
                propertyData.ValueRangeTo !== undefined) {
                var numValue = parseFloat(normalizedValue);
                if (numValue > propertyData.ValueRangeTo ||
                    numValue < propertyData.ValueRangeFrom) {
                    continue;
                }
            }
            if (propertyData.Values &&
                propertyData.Values.length &&
                !propertyData.Values.find(valueEqualsNormalized)) {
                continue;
            }
            targetable.push(articleInstance);
        }
    }
    catch (e_1_1) { e_1 = { error: e_1_1 }; }
    finally {
        try {
            if (objects_1_1 && !objects_1_1.done && (_a = objects_1.return)) _a.call(objects_1);
        }
        finally { if (e_1) throw e_1.error; }
    }
    return targetable;
};
