"use strict";

require("core-js/modules/es.promise");

require("core-js/modules/web.dom-collections.iterator");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.Loader = void 0;

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

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

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

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

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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 {
    rules,
    params,
    top,
    nomatch
  } = props;
  const [object, setObject] = (0, _react.useState)({});
  (0, _react.useEffect)(() => {
    let obj, rule, mounted;
    mounted = true;

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

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

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

      return;
    }

    const {
      load,
      results,
      finalize
    } = rule;

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

      if (nomatch) {
        nomatch(params);
      }

      return;
    }

    load(params).then(res => {
      if (mounted) return Promise.resolve(res);else return Promise.reject('unmounted');
    }).then(results).then(res => {
      if (mounted) return Promise.resolve(res);else return Promise.reject('unmounted');
    }).then(finalize).catch(err => {
      console.error(err);
    });
    return () => {
      mounted = false;
    };
  }, [rules, params]); // eslint-disable-line

  return /*#__PURE__*/_react.default.createElement(_LoadingCard.default, {
    object: object,
    top: top
  });
};

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