"use strict";

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

var _react = _interopRequireDefault(require("react"));

var _Link = _interopRequireDefault(require("./Link"));

var _internalRouterContextTypeCheck = _interopRequireDefault(require("./internalRouterContextTypeCheck"));

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

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

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; }

var EMPTY_STATE = {};

class StateLink extends _react.default.PureComponent {
  constructor() {
    super(...arguments);

    _defineProperty(this, "context", null);

    _defineProperty(this, "_element", null);

    _defineProperty(this, "setElement", element => {
      if (element) {
        this._element = element;
      }
    });
  }

  resolveUrl() {
    var _this$props = this.props,
        toIndex = _this$props.toIndex,
        state = _this$props.state;

    if (state && toIndex) {
      throw new Error('Passing both `state` and `toIndex` as props to StateLink is invalid');
    }

    if (!state && !toIndex) {
      // eslint-disable-next-line no-console
      console.error(new Error('No state passed to StateLink. If you want to link to an empty state, its better to use the the `toIndex` property'));
    }

    var nextState = toIndex ? EMPTY_STATE : state || EMPTY_STATE;
    return this.resolvePathFromState(nextState);
  }

  resolvePathFromState(state) {
    if (!this.context) throw new Error('StateLink: missing context value');

    if (!this.context.__internalRouter) {
      return "javascript://state@".concat(JSON.stringify(state));
    }

    return this.context.__internalRouter.resolvePathFromState(state);
  }

  focus() {
    if (this._element) {
      this._element.focus();
    }
  }

  render() {
    var _this$props2 = this.props,
        state = _this$props2.state,
        toIndex = _this$props2.toIndex,
        rest = _objectWithoutProperties(_this$props2, ["state", "toIndex"]);

    return /*#__PURE__*/_react.default.createElement(_Link.default, _extends({}, rest, {
      href: this.resolveUrl(),
      ref: this.setElement
    }));
  }

}

exports.default = StateLink;

_defineProperty(StateLink, "defaultProps", {
  replace: false,
  toIndex: false
});

_defineProperty(StateLink, "contextTypes", {
  __internalRouter: _internalRouterContextTypeCheck.default
});