import { __assign, __extends, __rest } from "tslib";
import * as React from "react";
import { withSessionContext } from "./SessionContext";
var PropertySubscription = /** @class */ (function (_super) {
    __extends(PropertySubscription, _super);
    function PropertySubscription() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.state = _this.buildState();
        return _this;
    }
    PropertySubscription.prototype.render = function () {
        if (this.state === null) {
            return null;
        }
        return this.props.children(this.state);
    };
    PropertySubscription.prototype.componentDidMount = function () {
        var _this = this;
        this.update();
        this.subscription = this.props.session.on("ItemConfigured", function () {
            return _this.update();
        });
    };
    PropertySubscription.prototype.componentWillUnmount = function () {
        if (this.subscription) {
            this.subscription.destroy();
        }
    };
    PropertySubscription.prototype.componentDidUpdate = function (prevProps) {
        if (prevProps.propertyKey !== this.props.propertyKey) {
            this.update();
        }
    };
    PropertySubscription.prototype.update = function () {
        this.setState(this.buildState());
    };
    PropertySubscription.prototype.buildState = function () {
        var _this = this;
        var property = this.props.session.getArticleProperty(this.props.comId, this.props.propertyKey);
        if (property === undefined) {
            throw new Error("Can not get unknown property \"" + this.props.propertyKey + "\" for \"" + this.props.comId + "\"");
        }
        var propertyKey = this.props.propertyKey;
        return __assign(__assign({}, property), { Values: this.props.createValues(property), setPropValue: function (newValue) {
                return _this.props.session.changeArticleProperty(_this.props.comId, propertyKey, newValue);
            } });
    };
    PropertySubscription.defaultProps = {
        createValues: function (_a) {
            var Values = _a.Values;
            return Values.filter(function (value) { return value.Visible !== false; });
        },
    };
    return PropertySubscription;
}(React.PureComponent));
var DecoratedSubscription = withSessionContext(PropertySubscription);
export default DecoratedSubscription;
export var withPropertySubscription = function (Component) { return function (_a) {
    var comId = _a.comId, propertyKey = _a.propertyKey, createValues = _a.createValues, childProps = __rest(_a, ["comId", "propertyKey", "createValues"]);
    return (React.createElement(DecoratedSubscription, { comId: comId, propertyKey: propertyKey, createValues: createValues }, function (_a) {
        var setPropValue = _a.setPropValue, property = __rest(_a, ["setPropValue"]);
        return (
        //@ts-ignore
        React.createElement(Component, __assign({}, childProps, { setPropValue: setPropValue, property: property })));
    }));
}; };
