"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useOrdering = useOrdering;
exports.useOrderingState = useOrderingState;
var _react = require("react");
var _orderBy = _interopRequireDefault(require("lodash-es/orderBy"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/**
 * @typedef {Object} OrderingState
 * @property {string} orderBy - property that's being used for ordering
 * @property {boolean} isAscendingOrder - is ordering ascending
 */

/**
 * @typedef {Ordering} Ordering
 * @property {string} orderBy - property that's being used for ordering
 * @property {boolean} isAscendingOrder - is ordering ascending
 * @property {function} setOrderBy - set ordering key
 * @property {function} setIsAscendingOrder - set ascending or descending order
 */

/**
 * Provides state and action for data ordering controls
 * If same key is set as `orderBy` order direction is reversed
 * Example:
 ```
  const { orderBy, isAscendingOrder, setOrderBy } = useOrderingState();
  const [data, setData] = useState(null);

  useEffect(() => {
    fetchStuff({
      orderBy,
      orderDirection: isAscendingOrder ? 'asc' : 'desc',
    }).then(setData);
  }, [orderBy, isAscendingOrder]);

  return (
    <SomeOrderedDataView
      orderBy={orderBy}
      isAscendingOrder={isAscendingOrder}
      onOrderChange={setOrderBy}
    />
  );
 ```
 * @param {OrderingState} initialState - initial values that will be applied
 * on mount
 * @returns {Ordering}
 */
function useOrderingState(config) {
  const orderKey = config?.orderBy ?? '';
  const initialDirection = config?.isAscendingOrder ?? false;
  const [orderBy, setOrderBy] = (0, _react.useState)(orderKey);
  const [isAscendingOrder, setIsAscendingOrder] = (0, _react.useState)(initialDirection);
  return (0, _react.useMemo)(() => ({
    orderBy,
    isAscendingOrder,
    setOrderBy: newOrderKey => {
      if (newOrderKey === orderBy) {
        setIsAscendingOrder(!isAscendingOrder);
      } else {
        setOrderBy(newOrderKey);
      }
    },
    setIsAscendingOrder
  }), [orderBy, isAscendingOrder]);
}
/**
 * Ordering hook that provides an array of ordered objects along
 * with state of ordering and actions to change it
 * Example:
 ```
  const unorderedItems = getItemsSomehow();
  const { items, orderBy, isAscendingOrder, setOrderBy } = useOrdering(
    unorderedItems
  );
  const headers = ['title', 'subTitle'];

  return (
    <div>
      {headers.map(header => (
        <div
          onClick={() => setOrderBy(header)}
          className={orderBy === header ? 'active' : ''}
        >
          {header}
          <FontAwesomeIcon icon={isAscendingOrder ? faSortUp : faSortDown} />
        </div>
      ))}
      {items.map(item => (
        <div>
          <div>{item.title}</div>
          <div>{item.subTitle}</div>
        </div>
      ))}
    </div>
  );
 ```
 *
 * @param {[object]} items - array of objects that needs to be ordered
 * @param {OrderingState} initialState - initial values for ordering
 * @param {function} orderFunction - custom ordering function
 * @returns
 */

function useOrdering(items, initialState, orderFunction = undefined) {
  const {
    orderBy,
    isAscendingOrder,
    setOrderBy,
    setIsAscendingOrder
  } = useOrderingState(initialState);
  const orderedItems = (0, _react.useMemo)(() => {
    if (!orderBy) {
      return items;
    }
    const orders = isAscendingOrder ? 'asc' : 'desc';
    if (orderFunction) {
      return (0, _orderBy.default)(items, item => orderFunction(item, orderBy, isAscendingOrder), orders);
    }
    return (0, _orderBy.default)(items, [orderBy], orders);
  }, [isAscendingOrder, orderBy, items, orderFunction]);
  return (0, _react.useMemo)(() => ({
    items: orderedItems,
    orderBy,
    isAscendingOrder,
    setOrderBy,
    setIsAscendingOrder
  }), [orderedItems, orderBy, isAscendingOrder, setOrderBy, setIsAscendingOrder]);
}