import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _defineProperty from "@babel/runtime/helpers/defineProperty";

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

function createSpaces(spaces) {
  return _objectSpread(_objectSpread({}, spaces), {}, {
    apply: function apply(key) {
      var _spaces$value, _DIRS_MAP$dir;

      var DIRS_MAP = {
        t: ["Top"],
        r: ["Right"],
        b: ["Bottom"],
        l: ["Left"],
        x: ["Right", "Left"],
        y: ["Top", "Bottom"]
      };
      var PROPS_MAP = {
        m: "margin",
        p: "padding"
      };

      var _key$split = key.split("-"),
          _key$split2 = _slicedToArray(_key$split, 2),
          fullProp = _key$split2[0],
          value = _key$split2[1];

      var _fullProp$split = fullProp.split(""),
          _fullProp$split2 = _slicedToArray(_fullProp$split, 2),
          prop = _fullProp$split2[0],
          dir = _fullProp$split2[1];

      var cssProp = PROPS_MAP[prop];

      if (!cssProp) {
        return {};
      }

      var valueNormalized = (_spaces$value = spaces[value]) !== null && _spaces$value !== void 0 ? _spaces$value : value;
      var dirs = (_DIRS_MAP$dir = DIRS_MAP[dir]) !== null && _DIRS_MAP$dir !== void 0 ? _DIRS_MAP$dir : [""];
      return dirs.reduce(function (acc, propDir) {
        acc[cssProp + propDir] = valueNormalized;
        return acc;
      }, {});
    }
  });
}

export default createSpaces;