"use strict";

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

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

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

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 Enum = /*#__PURE__*/function (_Type) {
  (0, _inherits2.default)(Enum, _Type);

  var _super = _createSuper(Enum);

  function Enum() {
    var _this;

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

    for (var _len = arguments.length, patterns = new Array(_len), _key = 0; _key < _len; _key++) {
      patterns[_key] = arguments[_key];
    }

    var pattern = patterns.length > 1 ? patterns : patterns[0];

    if (!(0, _tsFns.isArray)(pattern)) {
      throw new Error('[Enum]: pattern should be an array.');
    }

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

  (0, _createClass2.default)(Enum, [{
    key: "_decide",
    value: function _decide(value) {
      var pattern = this.pattern;
      var patterns = pattern;
      var tyerr = new _tyError.TyError();
      var errors = [];

      for (var i = 0, len = patterns.length; i < len; i++) {
        var _pattern = patterns[i]; // nested Type

        if ((0, _tsFns.isInstanceOf)(_pattern, _type.Type)) {
          if (this.isStrict && !_pattern.isStrict) {
            _pattern = _pattern.strict;
          } else if (!this.isStrict && this.isLoose && !_pattern.isStrict && _pattern.isLoose) {
            _pattern = _pattern.loose;
          }

          var error = _pattern.catch(value);

          if (!error) {
            return null;
          } else {
            errors.push(error);
          }
        } // normal validate
        else {
          var _error = this.validate(value, _pattern);

          if (!_error) {
            return null;
          } else {
            errors.push(_error);
          }
        }
      }

      if (errors.length) {
        tyerr.replace({
          type: 'notin',
          value: value,
          name: this.name,
          pattern: pattern,
          errors: errors
        });
      }

      tyerr.commit();
      return tyerr;
    }
  }]);
  return Enum;
}(_type.Type);

exports.Enum = Enum;

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

  var type = (0, _construct2.default)(Enum, patterns);
  return type;
}