"use strict";

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

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

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

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

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

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

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

var _tsFns = require("ts-fns");

var _type = require("./type.js");

var _tyError = require("./ty-error.js");

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2.default)(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2.default)(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2.default)(this, result); }; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

var Range = /*#__PURE__*/function (_Type) {
  (0, _inherits2.default)(Range, _Type);

  var _super = _createSuper(Range);

  function Range(pattern) {
    var _this;

    (0, _classCallCheck2.default)(this, Range);

    if (!(0, _tsFns.isObject)(pattern)) {
      throw new Error('[Range]: pattern should be an object.');
    }

    if (!(0, _tsFns.inObject)('min', pattern)) {
      throw new Error('[Range]: min should be in pattern.');
    }

    if (!(0, _tsFns.inObject)('max', pattern)) {
      throw new Error('[Range]: max should be in pattern.');
    }

    _this = _super.call(this, pattern);
    _this.name = 'Range';
    return _this;
  }

  (0, _createClass2.default)(Range, [{
    key: "_decide",
    value: function _decide(value) {
      var pattern = this.pattern;
      var tyerr = new _tyError.TyError();
      var min = pattern.min,
          max = pattern.max,
          _pattern$minBound = pattern.minBound,
          minBound = _pattern$minBound === void 0 ? true : _pattern$minBound,
          _pattern$maxBound = pattern.maxBound,
          maxBound = _pattern$maxBound === void 0 ? true : _pattern$maxBound;

      if (!(0, _tsFns.isNumber)(value)) {
        tyerr.replace({
          type: 'exception',
          value: value,
          name: this.name,
          pattern: pattern
        });
      } else if (minBound && value < min || !minBound && value <= min) {
        tyerr.replace({
          type: 'exception',
          value: value,
          name: this.name,
          pattern: pattern
        });
      } else if (maxBound && value > max || !maxBound && value >= max) {
        tyerr.replace({
          type: 'exception',
          value: value,
          name: this.name,
          pattern: pattern
        });
      } else {
        return null;
      }

      tyerr.commit();
      return tyerr.error();
    }
  }]);
  return Range;
}(_type.Type);

exports.Range = Range;

function range(pattern) {
  var type = new Range(pattern);
  return type;
}