"use strict";

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

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

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 _ty = require("./ty.js");

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

var _prototypes = require("./prototypes.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 Mapping = /*#__PURE__*/function (_Type) {
  (0, _inherits2.default)(Mapping, _Type);

  var _super = _createSuper(Mapping);

  function Mapping(pattern) {
    var _this;

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

    if (!_ty.Ty.is(pattern).of({
      key: _prototypes.Any,
      value: _prototypes.Any
    })) {
      throw new Error('[Mapping]: pattern should be an object with { key, value }.');
    }

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

  (0, _createClass2.default)(Mapping, [{
    key: "_decide",
    value: function _decide(value) {
      var _this2 = this;

      var pattern = this.pattern;
      var tyerr = new _tyError.TyError();
      var keyPattern = pattern.key,
          valuePattern = pattern.value;

      if (!(0, _tsFns.isObject)(value)) {
        tyerr.replace({
          type: 'exception',
          value: value,
          name: this.name,
          pattern: Object
        });
      } else {
        (0, _tsFns.each)(value, function (value, key) {
          var error = _this2.validate(key, keyPattern);

          if (error) {
            tyerr.add({
              type: 'illegal',
              error: error,
              pattern: keyPattern,
              key: key
            });
          }

          var error2 = _this2.validate(value, valuePattern);

          if (error2) {
            tyerr.add({
              type: 'exception',
              error: error2,
              key: key,
              pattern: valuePattern,
              value: value
            });
          }
        });
      }

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

exports.Mapping = Mapping;

function mapping(pattern) {
  var type = new Mapping(pattern);
  return type;
}