"use strict";

exports.__esModule = true;
exports.default = exports.Loader = void 0;

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _LoadingCard = _interopRequireDefault(require("./LoadingCard"));

var _useLoadingState = _interopRequireDefault(require("subfirelib/hooks/useLoadingState"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

const Loader = props => {
  const mountedRef = (0, _react.useRef)(null);
  const {
    rules,
    params,
    top,
    nomatch,
    pathname
  } = props;
  const [loadingContent, setLoadingContent] = (0, _useLoadingState.default)();
  (0, _react.useEffect)(() => {
    console.log('useEffect', mountedRef.current);
    if (mountedRef.current !== null) return;
    let obj, rule;
    mountedRef.current = true;

    for (let i = 0; i < rules.length; ++i) {
      obj = rules[i].match(params);

      if (!!obj) {
        setLoadingContent(obj);
        rule = rules[i];
        break;
      }
    }

    if (!obj) {
      if (nomatch) {
        nomatch(params);
      }

      return () => {
        mountedRef.current = false;
        setLoadingContent(() => null);
      };
    }

    const {
      load,
      results,
      finalize
    } = rule;

    if (!load || !results || !finalize) {
      console.error('rule for', params, ' not complete', rule);

      if (nomatch) {
        nomatch(params);
      } // reset to null so it can try again when the rules arrive


      return () => {
        mountedRef.current = null;
        setLoadingContent(() => null);
      };
    }

    load(params).then(res => {
      // hack - we're still in load but the component was unloaded...only happens *first* time
      if (mountedRef.current || pathname.startsWith('/load')) return Promise.resolve(res);else return Promise.reject('unmounted load');
    }).then(results).then(res => {
      if (mountedRef.current || pathname.startsWith('/load')) return Promise.resolve(res);else return Promise.reject('unmounted res');
    }).then(finalize).catch(err => {
      console.error(err);
    });
    return () => {
      console.log('unmounting');
      mountedRef.current = false;
      setLoadingContent(() => null);
    };
  }, [rules, params]); // eslint-disable-line

  return loadingContent && /*#__PURE__*/_react.default.createElement(_LoadingCard.default, {
    object: loadingContent,
    top: top
  }) || /*#__PURE__*/_react.default.createElement("span", null);
};

exports.Loader = Loader;
Loader.propTypes = {
  rules: _propTypes.default.array,
  params: _propTypes.default.object,
  top: _propTypes.default.number,
  nomatch: _propTypes.default.func,
  pathname: _propTypes.default.string
};
Loader.defaultProps = {
  top: 0
};
var _default = Loader;
exports.default = _default;