/**
 * amis-ui v6.12.0-beta.0
 * Copyright 2018-2025 fex
 */

import { __extends, __assign } from 'tslib';
import React__default from 'react';
import hoistNonReactStatic from 'hoist-non-react-statics';
import { destroy } from 'mobx-state-tree';
import { observer } from 'mobx-react';

function withStore(storeFactory) {
    return function (ComposedComponent) {
        var _a;
        ComposedComponent = observer(ComposedComponent);
        var result = hoistNonReactStatic((_a = /** @class */ (function (_super) {
                __extends(class_1, _super);
                function class_1() {
                    var _this = _super !== null && _super.apply(this, arguments) || this;
                    _this.store = storeFactory(_this.props);
                    _this.refFn = function (ref) {
                        _this.ref = ref;
                    };
                    return _this;
                }
                class_1.prototype.componentWillUnmount = function () {
                    this.store && destroy(this.store);
                    delete this.store;
                };
                class_1.prototype.getWrappedInstance = function () {
                    return this.ref;
                };
                class_1.prototype.render = function () {
                    var injectedProps = {
                        store: this.store
                    };
                    return (React__default.createElement(ComposedComponent, __assign({}, this.props, injectedProps, { ref: this.refFn })));
                };
                return class_1;
            }(React__default.Component)),
            _a.displayName = "WithStore(".concat(ComposedComponent.displayName || 'Unkown', ")"),
            _a.ComposedComponent = ComposedComponent,
            _a), ComposedComponent);
        return result;
    };
}

export { withStore as default, withStore };
