/** @internal NOTE: Internal APIs. Subject to change. Use of these APIs in production applications is not supported. */ /** */
import { HttpResponseType } from '../http';
import '../MapExtensions';
import { BimPropertySetReader } from './bim-property-set-reader';
import { telemetry } from '../Telemetry';
function addPropertySet(obj, name, set) {
    const current = obj.propertySets;
    if (Object.isFrozen(current)) {
        obj.propertySets = { ...current, [name]: set };
    }
    else {
        current[name] = set;
    }
}
function addQuantitySet(obj, name, set) {
    const current = obj.quantitySets;
    if (Object.isFrozen(current)) {
        obj.quantitySets = { ...current, [name]: set };
    }
    else {
        current[name] = set;
    }
}
export class BimPropertySetRepository {
    _bimApi;
    _propertyReader = new BimPropertySetReader();
    _isPropertySetUrlAdded = new Map();
    constructor(_bimApi) {
        this._bimApi = _bimApi;
    }
    assignPropertySetsToProducts(propertysetUrl, ifcObjects, ifcTypeObjects) {
        const key = propertysetUrl.href.toLowerCase();
        const loadingPromise = this._isPropertySetUrlAdded.get(key);
        if (loadingPromise) {
            return loadingPromise;
        }
        const p = this.fetchDecodeAndAssignPropertySetsToIfcObjects(propertysetUrl, ifcObjects, ifcTypeObjects);
        this._isPropertySetUrlAdded.set(key, p);
        return p;
    }
    clear() {
        this._isPropertySetUrlAdded.clear();
        this._propertyReader.clear();
    }
    async fetchDecodeAndAssignPropertySetsToIfcObjects(propertysetUrl, ifcObjects, ifcTypeObjects) {
        const bimIfcPropertiesDataResponse = await this._bimApi.get(propertysetUrl, HttpResponseType.json);
        if (bimIfcPropertiesDataResponse.status !== 200) {
            telemetry.trackTrace({ message: 'Failed to load property set data' }, {
                httpStatus: bimIfcPropertiesDataResponse.status,
                reason: bimIfcPropertiesDataResponse.statusText,
                url: propertysetUrl.href
            });
            return false;
        }
        const bimIfcPropertiesData = await bimIfcPropertiesDataResponse.value;
        const decodedPropertySets = this._propertyReader.decodePropertySets(bimIfcPropertiesData);
        for (const ifcObject of ifcObjects) {
            const propertySetIndicesForProduct = bimIfcPropertiesData.products[ifcObject.entityLabelInIfc] ?? [];
            for (const idx of propertySetIndicesForProduct) {
                const decoded = decodedPropertySets[idx];
                if (decoded.type === 'properties') {
                    let pset = decoded.value;
                    if (decoded.gidPropertyNames) {
                        pset = this._propertyReader.getT8PropertySetForProduct(ifcObject, decoded);
                    }
                    addPropertySet(ifcObject, decoded.name, pset);
                }
                else {
                    addQuantitySet(ifcObject, decoded.name, decoded.value);
                }
            }
        }
        if (bimIfcPropertiesData.types != null) {
            for (const ifcTypeObject of ifcTypeObjects) {
                const propertySetIndicesForProduct = bimIfcPropertiesData.types[ifcTypeObject.id] ?? [];
                for (const idx of propertySetIndicesForProduct) {
                    const decoded = decodedPropertySets[idx];
                    if (decoded.type === 'properties') {
                        addPropertySet(ifcTypeObject, decoded.name, decoded.value);
                    }
                    else {
                        addQuantitySet(ifcTypeObject, decoded.name, decoded.value);
                    }
                }
            }
        }
        return true;
    }
}
//# sourceMappingURL=bim-property-set-repository.js.map