"use strict";

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

var _ramda = require("ramda");

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }

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

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }

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

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

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_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; }

/*
 * requests_pathname_prefix is the new config parameter introduced in
 * dash==0.18.0. The previous versions just had url_base_pathname
 */
function urlBase(config) {
  var hasUrlBase = (0, _ramda.has)('url_base_pathname', config);
  var hasReqPrefix = (0, _ramda.has)('requests_pathname_prefix', config);

  if ((0, _ramda.type)(config) !== 'Object' || !hasUrlBase && !hasReqPrefix) {
    throw new Error("\n            Trying to make an API request but neither\n            \"url_base_pathname\" nor \"requests_pathname_prefix\"\n            is in `config`. `config` is: ", config);
  }

  var base = hasReqPrefix ? config.requests_pathname_prefix : config.url_base_pathname;
  return base.charAt(base.length - 1) === '/' ? base : base + '/';
}

var propsChildren = ['props', 'children']; // crawl a layout object or children array, apply a function on every object

var crawlLayout = function crawlLayout(object, func) {
  var currentPath = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
  var extraPath = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : undefined;

  if (Array.isArray(object)) {
    // children array
    object.forEach(function (child, i) {
      if (extraPath) {
        crawlLayout((0, _ramda.path)(extraPath, child), func, (0, _ramda.concat)(currentPath, (0, _ramda.concat)([i], extraPath)));
      } else {
        crawlLayout(child, func, (0, _ramda.append)(i, currentPath));
      }
    });
  } else if ((0, _ramda.type)(object) === 'Object') {
    func(object, currentPath);
    var children = (0, _ramda.path)(propsChildren, object);

    if (children) {
      var newPath = (0, _ramda.concat)(currentPath, propsChildren);
      crawlLayout(children, func, newPath);
    }

    var childrenProps = (0, _ramda.pathOr)([], [object.namespace, object.type], window.__dashprivate_childrenProps);
    childrenProps.forEach(function (childrenProp) {
      if (childrenProp.includes('[]')) {
        var _childrenProp$split$m = childrenProp.split('[]').map(function (p) {
          return p.split('.').filter(function (e) {
            return e;
          });
        }),
            _childrenProp$split$m2 = _slicedToArray(_childrenProp$split$m, 2),
            frontPath = _childrenProp$split$m2[0],
            backPath = _childrenProp$split$m2[1];

        var front = (0, _ramda.concat)(['props'], frontPath);
        var basePath = (0, _ramda.concat)(currentPath, front);
        crawlLayout((0, _ramda.path)(front, object), func, basePath, backPath);
      } else {
        var _newPath = (0, _ramda.concat)(currentPath, ['props'].concat(_toConsumableArray(childrenProp.split('.'))));

        crawlLayout((0, _ramda.path)(['props'].concat(_toConsumableArray(childrenProp.split('.'))), object), func, _newPath);
      }
    });
  }
}; // There are packages for this but it's simple enough, I just
// adapted it from https://gist.github.com/mudge/5830382


exports.crawlLayout = crawlLayout;

var EventEmitter = /*#__PURE__*/function () {
  function EventEmitter() {
    _classCallCheck(this, EventEmitter);

    this._ev = {};
  }

  _createClass(EventEmitter, [{
    key: "on",
    value: function on(event, listener) {
      var _this = this;

      var events = this._ev[event] = this._ev[event] || [];
      events.push(listener);
      return function () {
        return _this.removeListener(event, listener);
      };
    }
  }, {
    key: "removeListener",
    value: function removeListener(event, listener) {
      var events = this._ev[event];

      if (events) {
        var idx = events.indexOf(listener);

        if (idx > -1) {
          events.splice(idx, 1);
        }
      }
    }
  }, {
    key: "emit",
    value: function emit(event) {
      var _this2 = this;

      for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
        args[_key - 1] = arguments[_key];
      }

      var events = this._ev[event];

      if (events) {
        events.forEach(function (listener) {
          return listener.apply(_this2, args);
        });
      }
    }
  }, {
    key: "once",
    value: function once(event, listener) {
      var _this3 = this;

      var remove = this.on(event, function () {
        remove();

        for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
          args[_key2] = arguments[_key2];
        }

        listener.apply(_this3, args);
      });
    }
  }]);

  return EventEmitter;
}();

exports.EventEmitter = EventEmitter;