"use strict";

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

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

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

  var _super = _createSuper(Tupl);

  function Tupl(pattern) {
    var _this;

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

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

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

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

      if (!(0, _tsFns.isArray)(value)) {
        tyerr.replace({
          type: 'exception',
          value: value,
          name: this.name,
          pattern: pattern
        });
      } else if (this.isStrict && items.length != patterns.length) {
        tyerr.replace({
          type: 'dirty',
          value: value,
          name: this.name,
          pattern: pattern
        });
      } else {
        for (var i = 0, len = patterns.length; i < len; i++) {
          var _value = items[i];
          var _pattern = patterns[i];
          var index = i;
          var isRule = (0, _tsFns.isInstanceOf)(_pattern, _rule.Rule);

          if (isRule) {
            var rule = this.isStrict && !_pattern.isStrict ? _pattern.strict : !this.isStrict && this.isLoose && !_pattern.isStrict && _pattern.isLoose ? _pattern.loose : _pattern;
            var error = rule.catch(items, index);

            if (!error) {
              continue;
            } // after validate, the property may create by rule


            if (!(0, _tsFns.inObject)(index, items)) {
              tyerr.add({
                type: 'missing',
                index: index
              });
            } else {
              tyerr.add({
                error: error,
                index: index
              });
            }
          } else if (!(0, _tsFns.inObject)(index, items)) {
            tyerr.add({
              type: 'missing',
              index: index
            });
          } // nested type
          else 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) {
              tyerr.add({
                error: _error,
                index: index
              });
            }
          } // normal validate
          else {
            var _error2 = this.validate(_value, _pattern);

            if (_error2) {
              tyerr.add({
                error: _error2,
                index: index
              });
            }
          }
        }
      }

      tyerr.commit();
      return tyerr.count ? tyerr : null;
    }
  }]);
  return Tupl;
}(_type.Type);

exports.Tupl = Tupl;

function tupl(pattern) {
  var type = new Tupl(pattern);
  return type;
}