import _typeof from "@babel/runtime/helpers/typeof";
import * as React from 'react';
import { h as hoistNonReactStatics } from './hoist-non-react-statics.cjs.js';
var noop = function noop() {
  return undefined;
};
var noop$1 = noop;
var warn = function warn() {
  return undefined;
};
if (process.env.NODE_ENV !== 'production') {
  /**
   * Prints a warning in the console if it exists.
   *
   * @param {*} message
   */
  warn = function warn(message) {
    if ((typeof console === "undefined" ? "undefined" : _typeof(console)) !== undefined && typeof console.error === 'function') {
      console.error(message);
    } else {
      throw new Error(message);
    }
  };
}
var WithKeepAliveContext = /*#__PURE__*/React.createContext({});
var IdentificationContext = WithKeepAliveContext;
function getDisplayName(Component) {
  return Component.displayName || Component.name || 'Component';
}
var __assign$2 = undefined && undefined.__assign || function () {
  __assign$2 = Object.assign || function (t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];
      for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
    }
    return t;
  };
  return __assign$2.apply(this, arguments);
};
var withIdentificationContextConsumerDisplayName = 'withIdentificationContextConsumer';
function withIdentificationContextConsumer(Component) {
  var WithIdentificationContextConsumer = function WithIdentificationContextConsumer(props) {
    return /*#__PURE__*/React.createElement(IdentificationContext.Consumer, null, function (contextProps) {
      return /*#__PURE__*/React.createElement(Component, __assign$2({
        _identificationContextProps: contextProps
      }, props));
    });
  };
  WithIdentificationContextConsumer.displayName = withIdentificationContextConsumerDisplayName + "(" + getDisplayName(Component) + ")";
  return WithIdentificationContextConsumer;
}
undefined && undefined.__extends || function () {
  var _extendStatics = function extendStatics(d, b) {
    _extendStatics = Object.setPrototypeOf || {
      __proto__: []
    } instanceof Array && function (d, b) {
      d.__proto__ = b;
    } || function (d, b) {
      for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    };
    return _extendStatics(d, b);
  };
  return function (d, b) {
    _extendStatics(d, b);
    function __() {
      this.constructor = d;
    }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  };
}();
var __assign$1 = undefined && undefined.__assign || function () {
  __assign$1 = Object.assign || function (t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];
      for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
    }
    return t;
  };
  return __assign$1.apply(this, arguments);
};
undefined && undefined.__rest || function (s, e) {
  var t = {};
  for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
    if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  }
  return t;
};
undefined && undefined.__spreadArrays || function () {
  for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
  for (var r = Array(s), k = 0, i = 0; i < il; i++) for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) r[k] = a[j];
  return r;
};
var COMMAND;
(function (COMMAND) {
  COMMAND["UNACTIVATE"] = "unactivate";
  COMMAND["UNMOUNT"] = "unmount";
  COMMAND["ACTIVATE"] = "activate";
  COMMAND["CURRENT_UNMOUNT"] = "current_unmount";
  COMMAND["CURRENT_UNACTIVATE"] = "current_unactivate";
})(COMMAND || (COMMAND = {}));
var __assign = undefined && undefined.__assign || function () {
  __assign = Object.assign || function (t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];
      for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
    }
    return t;
  };
  return __assign.apply(this, arguments);
};
var __rest = undefined && undefined.__rest || function (s, e) {
  var t = {};
  for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
    if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  }
  return t;
};
var __spreadArrays = undefined && undefined.__spreadArrays || function () {
  for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
  for (var r = Array(s), k = 0, i = 0; i < il; i++) for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) r[k] = a[j];
  return r;
};
var bindLifecycleTypeName = '$$bindLifecycle';
function bindLifecycle(Component) {
  var WrappedComponent = Component.WrappedComponent || Component.wrappedComponent || Component;
  var _a = WrappedComponent.prototype,
    _b = _a.componentDidMount,
    componentDidMount = _b === void 0 ? noop$1 : _b,
    _c = _a.componentDidUpdate,
    componentDidUpdate = _c === void 0 ? noop$1 : _c,
    _d = _a.componentDidActivate,
    componentDidActivate = _d === void 0 ? noop$1 : _d,
    _e = _a.componentWillUnactivate,
    componentWillUnactivate = _e === void 0 ? noop$1 : _e,
    _f = _a.componentWillUnmount,
    componentWillUnmount = _f === void 0 ? noop$1 : _f,
    _g = _a.shouldComponentUpdate,
    shouldComponentUpdate = _g === void 0 ? noop$1 : _g;
  WrappedComponent.prototype.componentDidMount = function () {
    var _this = this;
    componentDidMount.call(this);
    this._needActivate = false;
    var _a = this.props,
      _b = _a._container,
      identification = _b.identification,
      eventEmitter = _b.eventEmitter,
      activated = _b.activated,
      keepAlive = _a.keepAlive;
    // Determine whether to execute the componentDidActivate life cycle of the current component based on the activation state of the KeepAlive components
    if (!activated && keepAlive !== false) {
      componentDidActivate.call(this);
    }
    eventEmitter.on([identification, COMMAND.ACTIVATE], this._bindActivate = function () {
      return _this._needActivate = true;
    }, true);
    eventEmitter.on([identification, COMMAND.UNACTIVATE], this._bindUnactivate = function () {
      componentWillUnactivate.call(_this);
      _this._unmounted = false;
    }, true);
    eventEmitter.on([identification, COMMAND.UNMOUNT], this._bindUnmount = function () {
      componentWillUnmount.call(_this);
      _this._unmounted = true;
    }, true);
  };
  // In order to be able to re-update after transferring the DOM, we need to block the first update.
  WrappedComponent.prototype.shouldComponentUpdate = function () {
    var args = [];
    for (var _i = 0; _i < arguments.length; _i++) {
      args[_i] = arguments[_i];
    }
    if (this._needActivate) {
      this.forceUpdate();
      return false;
    }
    return shouldComponentUpdate.call.apply(shouldComponentUpdate, __spreadArrays([this], args)) || true;
  };
  WrappedComponent.prototype.componentDidUpdate = function () {
    var args = [];
    for (var _i = 0; _i < arguments.length; _i++) {
      args[_i] = arguments[_i];
    }
    componentDidUpdate.call.apply(componentDidUpdate, __spreadArrays([this], args));
    if (this._needActivate) {
      this._needActivate = false;
      componentDidActivate.call(this);
    }
  };
  WrappedComponent.prototype.componentWillUnmount = function () {
    if (!this._unmounted) {
      componentWillUnmount.call(this);
    }
    var _a = this.props._container,
      identification = _a.identification,
      eventEmitter = _a.eventEmitter;
    eventEmitter.off([identification, COMMAND.ACTIVATE], this._bindActivate);
    eventEmitter.off([identification, COMMAND.UNACTIVATE], this._bindUnactivate);
    eventEmitter.off([identification, COMMAND.UNMOUNT], this._bindUnmount);
  };
  var BindLifecycleHOC = withIdentificationContextConsumer(function (_a) {
    var forwardRef = _a.forwardRef,
      _b = _a._identificationContextProps,
      identification = _b.identification,
      eventEmitter = _b.eventEmitter,
      activated = _b.activated,
      keepAlive = _b.keepAlive,
      extra = _b.extra,
      wrapperProps = __rest(_a, ["forwardRef", "_identificationContextProps"]);
    if (!identification) {
      warn('[React Keep Alive] You should not use bindLifecycle outside a <KeepAlive>.');
      return null;
    }
    return /*#__PURE__*/React.createElement(Component, __assign({}, extra, wrapperProps, {
      ref: forwardRef || noop$1,
      _container: {
        identification: identification,
        eventEmitter: eventEmitter,
        activated: activated,
        keepAlive: keepAlive
      }
    }));
  });
  var BindLifecycle = /*#__PURE__*/React.forwardRef(function (props, ref) {
    return /*#__PURE__*/React.createElement(BindLifecycleHOC, __assign({}, props, {
      forwardRef: ref
    }));
  });
  BindLifecycle.WrappedComponent = WrappedComponent;
  BindLifecycle.displayName = bindLifecycleTypeName + "(" + getDisplayName(Component) + ")";
  return hoistNonReactStatics(BindLifecycle, Component);
}
export { bindLifecycle as b };
//# sourceMappingURL=bindLifecycle.js.map
