"use strict";

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

var _combinationsGenerator = _interopRequireDefault(require("combinations-generator"));

var _cartesianProductGenerator = require("cartesian-product-generator");

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

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }

function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }

function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }

var convertUndefined = function convertUndefined(value) {
  return value === undefined ? null : value;
};

var handleOptional = function handleOptional(value, f) {
  return convertUndefined(value) === null ? null : f(value);
};

var handleProjectionItem = function handleProjectionItem(projectable) {
  return function (projectionRule) {
    return Number.isInteger(projectionRule) ? convertUndefined(projectable.slice(projectionRule)[0]) : Array.isArray(projectionRule) ? convertUndefined(projectable.slice.apply(projectable, _toConsumableArray(projectionRule))) : convertUndefined(projectable[projectionRule]);
  };
};

var __objectProjection__ = function __objectProjection__(left, right, optional, _) {
  var newObject = {};
  right.forEach(function (rule) {
    var key;
    var finalKey;

    if (rule.type === 'SimpleItem') {
      key = rule.value;
      finalKey = rule.alias || key;

      if (key in left) {
        newObject[finalKey] = left[key];
      } else {
        newObject[finalKey] = null;
      }
    }

    if (rule.type === 'FragmentItem') {
      Object.assign(newObject, _[rule.value](left));
    }

    if (rule.type === 'RecursiveItem') {
      key = rule.name;
      finalKey = rule.alias || key;
      var rules = rule.value.value;

      if (key in left) {
        newObject[finalKey] = __objectProjection__(left[key], rules, false, _);
      } else {
        newObject[finalKey] = null;
      }
    }
  });
  return newObject;
};

var handleProjection = function handleProjection(projectable) {
  return function (projectionRules) {
    return projectionRules.map(handleProjectionItem(projectable));
  };
};

var _default = {
  objectify: function objectify(input) {
    return input.reduce(function (a, b) {
      a[b[0]] = b[1];
      return a;
    }, {});
  },
  __opt__: handleOptional,
  __spread__: function __spread__(input) {
    return Array.isArray(input) ? input : typeof input === 'string' || input instanceof String ? input.split('') : Object.entries(input);
  },
  projection: function projection(left, right, optinal) {
    return right.length === 1 ? handleOptional(left, function () {
      return handleProjection(left)(right)[0];
    }) : handleOptional(left, function () {
      return handleProjection(left)(right);
    });
  },
  __objectProjection__: __objectProjection__,
  split: function split(separator) {
    return function (input) {
      return input.split(separator);
    };
  },
  join: function join(separator) {
    return function (input) {
      return input.join(separator);
    };
  },
  map: function map(f) {
    return function (input) {
      return input.map(f);
    };
  },
  sortBy: function sortBy(f) {
    return function (input) {
      return input.slice().sort(function (a, b) {
        return f(a) < f(b) ? -1 : f(a) > f(b) ? 1 : 0;
      });
    };
  },
  has: function has(key) {
    return function (input) {
      return key in input;
    };
  },
  filter: function filter(f) {
    return function (input) {
      return input.filter(f);
    };
  },
  get: function get(variable) {
    return this[variable];
  },
  assign: function assign(variable, value, context) {
    return Object.assign({}, context, _defineProperty({}, variable, value));
  },
  reverse: function reverse(input) {
    return input.slice().reverse();
  },
  reduce: function reduce(_ref) {
    var _ref2 = _slicedToArray(_ref, 2),
        f = _ref2[0],
        x = _ref2[1];

    return function (input) {
      return input.reduce(function (a, b) {
        return f(a)(b);
      }, x);
    };
  },
  length: function length(input) {
    return input.length;
  },
  keys: function keys(input) {
    return Object.keys(input);
  },
  values: function values(input) {
    return Object.values(input);
  },
  combinations: function combinations(r) {
    return function (input) {
      return Array.from((0, _combinationsGenerator.default)(input, r));
    };
  },
  product: function product(input) {
    return Array.from(_cartesianProductGenerator.product.apply(void 0, _toConsumableArray(input)));
  },
  error: function error(message) {
    return function (input) {
      throw new Error(message);
    };
  }
};
exports.default = _default;