"use strict";

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

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

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

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

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

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

var _viewport = _interopRequireDefault(require("../viewports/viewport"));

var _positions = require("../utils/positions");

var _deepEqual = require("../utils/deep-equal");

var _assert = _interopRequireDefault(require("../utils/assert"));

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (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 = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

var View = function () {
  function View(props) {
    (0, _classCallCheck2.default)(this, View);
    this.id = void 0;
    this.viewportInstance = void 0;
    this._x = void 0;
    this._y = void 0;
    this._width = void 0;
    this._height = void 0;
    this._padding = void 0;
    this.props = void 0;

    var _ref = props || {},
        id = _ref.id,
        _ref$x = _ref.x,
        x = _ref$x === void 0 ? 0 : _ref$x,
        _ref$y = _ref.y,
        y = _ref$y === void 0 ? 0 : _ref$y,
        _ref$width = _ref.width,
        width = _ref$width === void 0 ? '100%' : _ref$width,
        _ref$height = _ref.height,
        height = _ref$height === void 0 ? '100%' : _ref$height,
        _ref$padding = _ref.padding,
        padding = _ref$padding === void 0 ? null : _ref$padding,
        viewportInstance = _ref.viewportInstance;

    (0, _assert.default)(!viewportInstance || viewportInstance instanceof _viewport.default);
    this.viewportInstance = viewportInstance;
    this.id = id || this.constructor.displayName || 'view';
    this.props = _objectSpread(_objectSpread({}, props), {}, {
      id: this.id
    });
    this._x = (0, _positions.parsePosition)(x);
    this._y = (0, _positions.parsePosition)(y);
    this._width = (0, _positions.parsePosition)(width);
    this._height = (0, _positions.parsePosition)(height);
    this._padding = padding && {
      left: (0, _positions.parsePosition)(padding.left || 0),
      right: (0, _positions.parsePosition)(padding.right || 0),
      top: (0, _positions.parsePosition)(padding.top || 0),
      bottom: (0, _positions.parsePosition)(padding.bottom || 0)
    };
    this.equals = this.equals.bind(this);
    Object.seal(this);
  }

  var _proto = View.prototype;

  _proto.equals = function equals(view) {
    if (this === view) {
      return true;
    }

    if (this.viewportInstance) {
      return view.viewportInstance ? this.viewportInstance.equals(view.viewportInstance) : false;
    }

    return this.ViewportType === view.ViewportType && (0, _deepEqual.deepEqual)(this.props, view.props, 2);
  };

  _proto.makeViewport = function makeViewport(_ref2) {
    var width = _ref2.width,
        height = _ref2.height,
        viewState = _ref2.viewState;

    if (this.viewportInstance) {
      return this.viewportInstance;
    }

    viewState = this.filterViewState(viewState);
    var viewportDimensions = this.getDimensions({
      width: width,
      height: height
    });

    if (!viewportDimensions.height || !viewportDimensions.width) {
      return null;
    }

    return new this.ViewportType(_objectSpread(_objectSpread(_objectSpread({}, viewState), this.props), viewportDimensions));
  };

  _proto.getViewStateId = function getViewStateId() {
    var viewState = this.props.viewState;

    if (typeof viewState === 'string') {
      return viewState;
    }

    return (viewState === null || viewState === void 0 ? void 0 : viewState.id) || this.id;
  };

  _proto.filterViewState = function filterViewState(viewState) {
    if (this.props.viewState && (0, _typeof2.default)(this.props.viewState) === 'object') {
      if (!this.props.viewState.id) {
        return this.props.viewState;
      }

      var newViewState = _objectSpread({}, viewState);

      for (var key in this.props.viewState) {
        if (key !== 'id') {
          newViewState[key] = this.props.viewState[key];
        }
      }

      return newViewState;
    }

    return viewState;
  };

  _proto.getDimensions = function getDimensions(_ref3) {
    var width = _ref3.width,
        height = _ref3.height;
    var dimensions = {
      x: (0, _positions.getPosition)(this._x, width),
      y: (0, _positions.getPosition)(this._y, height),
      width: (0, _positions.getPosition)(this._width, width),
      height: (0, _positions.getPosition)(this._height, height)
    };

    if (this._padding) {
      dimensions.padding = {
        left: (0, _positions.getPosition)(this._padding.left, width),
        top: (0, _positions.getPosition)(this._padding.top, height),
        right: (0, _positions.getPosition)(this._padding.right, width),
        bottom: (0, _positions.getPosition)(this._padding.bottom, height)
      };
    }

    return dimensions;
  };

  (0, _createClass2.default)(View, [{
    key: "controller",
    get: function get() {
      var opts = this.props.controller;

      if (!opts) {
        return null;
      }

      if (opts === true) {
        return {
          type: this.ControllerType
        };
      }

      if (typeof opts === 'function') {
        return {
          type: opts
        };
      }

      return _objectSpread({
        type: this.ControllerType
      }, opts);
    }
  }]);
  return View;
}();

exports.default = View;
//# sourceMappingURL=view.js.map