"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var React = require("react");
var igd_core_1 = require("@intelligentgraphics/igd.core");
var Util_1 = require("../Util");
var Context_1 = require("../Context");
/**
 * Provides the current data and manipulation methods for a single property editor property
 *
 * @param {string} identifier
 * @returns {(PropertyViewProps | undefined)}
 */
exports.usePropertyEditorProperty = function (identifier) {
    var context = Context_1.usePropertyEditorContext();
    // since the property editor only stores the direct properties of articles, we first need to find the root property name,
    // so that we can descend its tree and find the actual property
    var rootPropertyIdentifier = React.useMemo(function () {
        var _a = Util_1.parsePropertyIdentifier(identifier), groupId = _a.groupId, propertyKey = _a.propertyKey;
        var _b = tslib_1.__read(igd_core_1.IGXE.getPropertyNamesFromKey(propertyKey), 1), rootPropertyName = _b[0];
        return Util_1.createPropertyIdentifier(groupId, rootPropertyName);
    }, [identifier]);
    var property = React.useMemo(function () {
        var propertyKey = Util_1.parsePropertyIdentifier(identifier).propertyKey;
        var rootProperty = context.properties[rootPropertyIdentifier];
        if (rootProperty === undefined) {
            return undefined;
        }
        return igd_core_1.IGXE.findProperty([rootProperty], propertyKey);
    }, [identifier, context.properties]);
    // values are always set directly on the root property
    var functions = React.useMemo(function () { return ({
        setPropertyValue: function (value) {
            return context.setPropertyValue(rootPropertyIdentifier, value);
        },
        applyToAll: function () { return context.applyToAll(rootPropertyIdentifier); },
    }); }, [rootPropertyIdentifier]);
    if (property === undefined) {
        return undefined;
    }
    return tslib_1.__assign(tslib_1.__assign({}, property), functions);
};
var Property = function (props) {
    var data = exports.usePropertyEditorProperty(props.identifier);
    if (data === undefined) {
        return null;
    }
    return props.children(data);
};
Property.displayName = "Property";
exports.default = Property;
