import { __assign, __values } from "tslib";
import { PropertyUtil, IGXE, } from "@intelligentgraphics/igd.productdata";
import * as Units from "../Units";
import { PropertyEditorSubPropertyBehaviour, } from "./Component";
var DELIMITER = "||";
export var createPropertyIdentifier = function (groupId, propertyKey) { return groupId + DELIMITER + propertyKey; };
export var parsePropertyIdentifier = function (propertyIdentifier) {
    var propertyIdentifierParts = propertyIdentifier.split(DELIMITER);
    if (propertyIdentifierParts.length !== 2) {
        throw new Error("Detected invalid property name. All property names, need to be prepended by their group id inside the property editor context");
    }
    return {
        groupId: propertyIdentifierParts[0],
        propertyKey: propertyIdentifierParts[1],
    };
};
// TODO: implement ShowInline sub property behaviour
/**
 * Creates property groups, a property map and a reference mapping of properties -> affected comIds
 *
 * @param {CreatePropertiesParams} {
 * 	propertyList,
 * 	transferTargets,
 * 	units
 * }
 * @returns
 */
export var createComponentState = function (_a) {
    var e_1, _b;
    var propertyList = _a.propertyList, transferTargets = _a.transferTargets, units = _a.units, subPropertyBehaviour = _a.subPropertyBehaviour;
    var groups = [];
    var resultProperties = {};
    var groupReferences = {};
    try {
        for (var propertyList_1 = __values(propertyList), propertyList_1_1 = propertyList_1.next(); !propertyList_1_1.done; propertyList_1_1 = propertyList_1.next()) {
            var _c = propertyList_1_1.value, groupId = _c.groupId, groupLabel = _c.groupLabel, references = _c.references, properties = _c.properties, isMerged = _c.isMerged;
            var groupProperties = [];
            for (var index = 0; index < properties.length; index++) {
                var property = properties[index];
                var newProperty = createProperty(property.Key, property, groupId, transferTargets, subPropertyBehaviour, units);
                groupProperties.push(newProperty.Identifier);
                resultProperties[newProperty.Identifier] = newProperty;
            }
            var group = {
                id: groupId,
                label: groupLabel || groupId,
                properties: groupProperties,
                isMerged: isMerged,
            };
            if (groupReferences[groupId]) {
                throw new Error("It seems like one groupId has been used twice. THIS IS UNACCEPTABLE");
            }
            groupReferences[groupId] = references;
            groups.push(group);
        }
    }
    catch (e_1_1) { e_1 = { error: e_1_1 }; }
    finally {
        try {
            if (propertyList_1_1 && !propertyList_1_1.done && (_b = propertyList_1.return)) _b.call(propertyList_1);
        }
        finally { if (e_1) throw e_1.error; }
    }
    return {
        groups: groups,
        groupReferences: groupReferences,
        properties: resultProperties,
    };
};
var createProperty = function (rootPropertyKey, property, groupId, transferTargets, subPropertyBehaviour, units) {
    var newProperty = __assign(__assign({}, property), { Transferable: property.Values.length > 0 &&
            Boolean(transferTargets.length > 0 &&
                property.Value !== undefined &&
                PropertyUtil.findApplyTargets(transferTargets, rootPropertyKey, property.Value).length > 0), ValueText: [], SubProperties: [], Identifier: createPropertyIdentifier(groupId, property.Key) });
    if (units) {
        var convertedProperty = Units.convertProperty(newProperty, newProperty.ValueUnit, units[Units.getGroupOfUnit(newProperty.ValueUnit)]);
        if (convertedProperty !== undefined) {
            newProperty = convertedProperty;
        }
    }
    if (property.Value) {
        if (property.ValueType === IGXE.EPropertyType.Float ||
            property.ValueType === IGXE.EPropertyType.Int) {
            newProperty.ValueText = [newProperty.Value.toString()];
        }
        else {
            var foundValue = property.Values &&
                property.Values.find(function (val) { return val.Value === property.Value; });
            if (foundValue && foundValue.Text) {
                newProperty.ValueText = foundValue.Text;
            }
        }
    }
    if (subPropertyBehaviour === PropertyEditorSubPropertyBehaviour.Depth &&
        property.SubProperties.length > 0) {
        newProperty.SubProperties = property.SubProperties.map(function (property) {
            return createProperty(rootPropertyKey, property, groupId, transferTargets, subPropertyBehaviour);
        });
    }
    return newProperty;
};
