/**
 * amis-ui v6.14.0-beta.1
 * Copyright 2018-2025 fex
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var tslib = require('tslib');
var React = require('react');
var hoistNonReactStatic = require('hoist-non-react-statics');
var mobxStateTree = require('mobx-state-tree');
var mobxReact = require('mobx-react');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
var hoistNonReactStatic__default = /*#__PURE__*/_interopDefaultLegacy(hoistNonReactStatic);

var __react_jsx__ = require('react');
var _J$X_ = (__react_jsx__["default"] || __react_jsx__).createElement;
(__react_jsx__["default"] || __react_jsx__).Fragment;
function withStore(storeFactory) {
    return function (ComposedComponent) {
        var _a;
        ComposedComponent = mobxReact.observer(ComposedComponent);
        var result = hoistNonReactStatic__default["default"]((_a = /** @class */ (function (_super) {
                tslib.__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 && mobxStateTree.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 (_J$X_(ComposedComponent, tslib.__assign({}, this.props, injectedProps, { ref: this.refFn })));
                };
                return class_1;
            }(React__default["default"].Component)),
            _a.displayName = "WithStore(".concat(ComposedComponent.displayName || 'Unkown', ")"),
            _a.ComposedComponent = ComposedComponent,
            _a), ComposedComponent);
        return result;
    };
}

exports["default"] = withStore;
exports.withStore = withStore;
