"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.ContextPerPageSelector = exports.ContextPagination = exports.ContextHighlight = exports.ContextTH = exports.ContextChild = exports.SetupContext = exports.connectUI = exports.connectSetup = exports.PerPageSelector = exports.Pagination = exports.TH = exports.Highlight = exports.highlight = void 0;

var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));

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

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

var _classnames = _interopRequireDefault(require("classnames"));

var _reactRedux = require("react-redux");

var _utils = require("./utils");

var highlight = function highlight(str, filter) {
  if (str === void 0) {
    str = '';
  }

  if (filter === void 0) {
    filter = '';
  }

  return str.toString().replace((0, _utils.createRegExp)(filter), '<strong>$1</strong>');
};

exports.highlight = highlight;
var _highlight = highlight;

var Highlight = function Highlight(_ref) {
  var children = _ref.children,
      filter = _ref.filter,
      highlight = _ref.highlight,
      props = (0, _objectWithoutPropertiesLoose2.default)(_ref, ["children", "filter", "highlight"]);
  var hl = highlight || _highlight;
  return _react.default.createElement("span", (0, _extends2.default)({}, props, {
    dangerouslySetInnerHTML: {
      __html: hl(children, filter)
    }
  }));
};

exports.Highlight = Highlight;
Highlight.propTypes = {
  children: _propTypes.default.node,
  filter: _propTypes.default.string,
  highlight: _propTypes.default.func
};
Highlight.defaultProps = {
  children: null,
  filter: '',
  highlight: null
};

var TH = function TH(_ref2) {
  var _ref2$sort = _ref2.sort,
      by = _ref2$sort.by,
      reverse = _ref2$sort.reverse,
      onSetSort = _ref2.onSetSort,
      nextBy = _ref2.by,
      children = _ref2.children,
      className = _ref2.className,
      props = (0, _objectWithoutPropertiesLoose2.default)(_ref2, ["sort", "onSetSort", "by", "children", "className"]);
  var isCurrent = nextBy === by; //TODO Add ability to customize onClick

  var onClick = function onClick(e) {
    e.preventDefault();
    onSetSort(nextBy, isCurrent ? !reverse : false);
  };

  className = (0, _classnames.default)('sortable', {
    'order-asc': isCurrent && !reverse,
    'order-desc': isCurrent && reverse
  }, className);
  var iconClassName = (0, _classnames.default)('rc-icon', isCurrent && reverse ? 'rc-icon-chevron-up-small' : 'rc-icon-chevron-down-small', 'sorter-icon', {
    'list-hover-arrow': !isCurrent
  });
  return _react.default.createElement("th", (0, _extends2.default)({
    onClick: onClick
  }, props, {
    className: className
  }), children, _react.default.createElement("span", {
    className: (0, _classnames.default)(iconClassName)
  }));
};

exports.TH = TH;
TH.propTypes = {
  children: _propTypes.default.node,
  sort: _propTypes.default.shape({
    by: _propTypes.default.string.isRequired,
    reverse: _propTypes.default.bool.isRequired
  }),
  by: _propTypes.default.string.isRequired,
  className: _propTypes.default.string,
  onSetSort: _propTypes.default.func.isRequired
};
TH.defaultProps = {
  children: null,
  sort: null,
  className: null
};

var Pagination = function Pagination(_ref3) {
  var loading = _ref3.loading,
      filteredTotal = _ref3.filteredTotal,
      _ref3$pagination = _ref3.pagination,
      page = _ref3$pagination.page,
      perPage = _ref3$pagination.perPage,
      onSetPage = _ref3.onSetPage,
      className = _ref3.className,
      props = (0, _objectWithoutPropertiesLoose2.default)(_ref3, ["loading", "filteredTotal", "pagination", "onSetPage", "className"]);

  var onPageClick = function onPageClick(p, e) {
    e.preventDefault();
    onSetPage(p);
  };

  var totalPages = Math.ceil(filteredTotal / perPage);
  var pages = [];

  for (var i = 1; i < totalPages + 1; i++) {
    pages.push(_react.default.createElement("button", {
      className: (0, _classnames.default)('btn btn-default', {
        active: page === i
      }),
      key: i,
      onClick: onPageClick.bind(null, i),
      disabled: loading
    }, i));
  }

  return _react.default.createElement("span", (0, _extends2.default)({
    className: (0, _classnames.default)('btn-group', className)
  }, props), pages);
};

exports.Pagination = Pagination;
Pagination.propTypes = {
  pagination: _propTypes.default.shape({
    page: _propTypes.default.number.isRequired,
    perPage: _propTypes.default.number.isRequired
  }).isRequired,
  loading: _propTypes.default.bool,
  filteredTotal: _propTypes.default.number.isRequired,
  className: _propTypes.default.string,
  onSetPage: _propTypes.default.func.isRequired
};
Pagination.defaultProps = {
  loading: false,
  className: null
};

var PerPageSelector = function PerPageSelector(_ref4) {
  var loading = _ref4.loading,
      _ref4$pagination = _ref4.pagination,
      perPage = _ref4$pagination.perPage,
      availablePerPage = _ref4$pagination.availablePerPage,
      onSetPerPage = _ref4.onSetPerPage,
      props = (0, _objectWithoutPropertiesLoose2.default)(_ref4, ["loading", "pagination", "onSetPerPage"]);

  var onChange = function onChange(e) {
    onSetPerPage(parseInt(e.target.value, 10));
  };

  return _react.default.createElement("select", (0, _extends2.default)({
    className: "form-control",
    onChange: onChange,
    value: perPage,
    disabled: loading
  }, props), availablePerPage.map(function (p, i) {
    return _react.default.createElement("option", {
      key: i,
      value: p
    }, p === _utils.ALL_PAGES ? 'All' : p);
  }));
};

exports.PerPageSelector = PerPageSelector;
PerPageSelector.propTypes = {
  pagination: _propTypes.default.shape({
    availablePerPage: _propTypes.default.arrayOf(_propTypes.default.number).isRequired,
    perPage: _propTypes.default.number.isRequired
  }).isRequired,
  loading: _propTypes.default.bool,
  onSetPerPage: _propTypes.default.func.isRequired
};
PerPageSelector.defaultProps = {
  loading: false
};
/**
 * @param {Setup} setup
 * @param {function} [propsMapper]
 * @param {function} [dispatchMapper]
 * @return {function(*)}
 */

var connectSetup = function connectSetup(setup, propsMapper, dispatchMapper) {
  if (propsMapper === void 0) {
    propsMapper = function propsMapper(props) {
      return props;
    };
  }

  if (dispatchMapper === void 0) {
    dispatchMapper = function dispatchMapper(props) {
      return props;
    };
  }

  return function (Cmp) {
    if (!propsMapper) {
      propsMapper = function propsMapper(props) {
        return props;
      };
    }

    if (!dispatchMapper) {
      dispatchMapper = function dispatchMapper(props) {
        return props;
      };
    }

    return (0, _reactRedux.connect)(function (state) {
      return propsMapper({
        filter: setup.getFilter(state),
        fetched: setup.getFetched(state),
        loading: setup.getLoading(state),
        error: setup.getError(state),
        items: setup.getItems(state),
        total: setup.getTotal(state),
        filteredTotal: setup.getFilteredTotal(state),
        sort: setup.getSort(state),
        pagination: setup.getPagination(state),
        params: setup.getParams(state),
        setup: setup //TODO Unnecessary?

      }, setup);
    }, dispatchMapper({
      onLoad: setup.load,
      onSetParams: setup.setParams,
      onSetSort: setup.setSort,
      onSetPage: setup.setPage,
      onSetPerPage: setup.setPerPage,
      onSetFilter: setup.setFilter
    }, setup))(Cmp);
  };
};
/**
 * @param {Setup} setup
 * @param {function} [mapper]
 * @return {function(*)}
 */


exports.connectSetup = connectSetup;

var connectUI = function connectUI(setup, mapper) {
  if (mapper === void 0) {
    mapper = function mapper(props) {
      return props;
    };
  }

  var UI = mapper({
    TH: connectSetup(setup)(TH),
    Highlight: connectSetup(setup)(Highlight),
    PerPageSelector: connectSetup(setup)(PerPageSelector),
    Pagination: connectSetup(setup)(Pagination)
  }, setup);
  return function (Cmp) {
    return function (props) {
      return _react.default.createElement(Cmp, (0, _extends2.default)({}, props, UI));
    };
  };
};

exports.connectUI = connectUI;

var SetupContext =
/*#__PURE__*/
function (_React$Component) {
  (0, _inheritsLoose2.default)(SetupContext, _React$Component);

  function SetupContext() {
    return _React$Component.apply(this, arguments) || this;
  }

  var _proto = SetupContext.prototype;

  _proto.getChildContext = function getChildContext() {
    return {
      setup: this.props.setup //TODO Multiple named setups via this.props.setup.prefix()

    };
  };

  _proto.render = function render() {
    return this.props.children;
  };

  return SetupContext;
}(_react.default.Component);

exports.SetupContext = SetupContext;
(0, _defineProperty2.default)(SetupContext, "propTypes", {
  setup: _propTypes.default.any.isRequired,
  children: _propTypes.default.any.isRequired
});
(0, _defineProperty2.default)(SetupContext, "childContextTypes", {
  setup: _propTypes.default.any
});

var ContextChild =
/*#__PURE__*/
function (_React$Component2) {
  (0, _inheritsLoose2.default)(ContextChild, _React$Component2);

  function ContextChild() {
    return _React$Component2.apply(this, arguments) || this;
  }

  var _proto2 = ContextChild.prototype;

  _proto2.componentWillMount = function componentWillMount() {
    this.component = this.connectComponent();
  }
  /**
   * @abstract
   * @return {React.Component}
   */
  ;

  _proto2.getComponent = function getComponent() {
    return null;
  };

  _proto2.connectComponent = function connectComponent() {
    return connectSetup(this.context.setup)(this.getComponent());
  };

  _proto2.render = function render() {
    var Cmp = this.component;
    var _this$props = this.props,
        children = _this$props.children,
        props = (0, _objectWithoutPropertiesLoose2.default)(_this$props, ["children"]);
    return _react.default.createElement(Cmp, props, children);
  };

  return ContextChild;
}(_react.default.Component);

exports.ContextChild = ContextChild;
(0, _defineProperty2.default)(ContextChild, "contextTypes", {
  setup: _propTypes.default.any
});
(0, _defineProperty2.default)(ContextChild, "propTypes", {
  children: _propTypes.default.node
});
(0, _defineProperty2.default)(ContextChild, "defaultProps", {
  children: null
});

var ContextTH =
/*#__PURE__*/
function (_ContextChild) {
  (0, _inheritsLoose2.default)(ContextTH, _ContextChild);

  function ContextTH() {
    return _ContextChild.apply(this, arguments) || this;
  }

  var _proto3 = ContextTH.prototype;

  _proto3.getComponent = function getComponent() {
    return TH;
  };

  return ContextTH;
}(ContextChild);

exports.ContextTH = ContextTH;

var ContextHighlight =
/*#__PURE__*/
function (_ContextChild2) {
  (0, _inheritsLoose2.default)(ContextHighlight, _ContextChild2);

  function ContextHighlight() {
    return _ContextChild2.apply(this, arguments) || this;
  }

  var _proto4 = ContextHighlight.prototype;

  _proto4.getComponent = function getComponent() {
    return Highlight;
  };

  return ContextHighlight;
}(ContextChild);

exports.ContextHighlight = ContextHighlight;

var ContextPagination =
/*#__PURE__*/
function (_ContextChild3) {
  (0, _inheritsLoose2.default)(ContextPagination, _ContextChild3);

  function ContextPagination() {
    return _ContextChild3.apply(this, arguments) || this;
  }

  var _proto5 = ContextPagination.prototype;

  _proto5.getComponent = function getComponent() {
    return Pagination;
  };

  return ContextPagination;
}(ContextChild);

exports.ContextPagination = ContextPagination;

var ContextPerPageSelector =
/*#__PURE__*/
function (_ContextChild4) {
  (0, _inheritsLoose2.default)(ContextPerPageSelector, _ContextChild4);

  function ContextPerPageSelector() {
    return _ContextChild4.apply(this, arguments) || this;
  }

  var _proto6 = ContextPerPageSelector.prototype;

  _proto6.getComponent = function getComponent() {
    return PerPageSelector;
  };

  return ContextPerPageSelector;
}(ContextChild);

exports.ContextPerPageSelector = ContextPerPageSelector;