import { PropertyUtil, IGXE, } from "@intelligentgraphics/igd.productdata";
import * as Units from "../Units";
import { PropertyEditorSubPropertyBehaviour, } from "./Component";
const DELIMITER = "||";
export const createPropertyIdentifier = (groupId, propertyKey) => groupId + DELIMITER + propertyKey;
export const parsePropertyIdentifier = (propertyIdentifier) => {
    const 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 const createComponentState = ({ propertyList, transferTargets, units, subPropertyBehaviour, }) => {
    const groups = [];
    const resultProperties = {};
    const groupReferences = {};
    for (const { groupId, groupLabel, references, properties, isMerged, } of propertyList) {
        const groupProperties = [];
        for (let index = 0; index < properties.length; index++) {
            const property = properties[index];
            const newProperty = createProperty(property.Key, property, groupId, transferTargets, subPropertyBehaviour, units);
            groupProperties.push(newProperty.Identifier);
            resultProperties[newProperty.Identifier] = newProperty;
        }
        const group = {
            id: groupId,
            label: groupLabel || groupId,
            properties: groupProperties,
            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);
    }
    return {
        groups,
        groupReferences,
        properties: resultProperties,
    };
};
const createProperty = (rootPropertyKey, property, groupId, transferTargets, subPropertyBehaviour, units) => {
    let newProperty = {
        ...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) {
        const 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 {
            const foundValue = property.Values &&
                property.Values.find(val => 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(property => createProperty(rootPropertyKey, property, groupId, transferTargets, subPropertyBehaviour));
    }
    return newProperty;
};
