"use strict";

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

var yup = _interopRequireWildcard(require("yup"));

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

var _Option = require("../Option");

var _constraints = require("../constraints");

var _resolvers;

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

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 resolvers = (_resolvers = {}, _defineProperty(_resolvers, _type.type.string, function () {
  return yup.string();
}), _defineProperty(_resolvers, _type.type.number, function (typeErrorMessage) {
  return yup.number().typeError(typeErrorMessage || 'Value must be a number');
}), _defineProperty(_resolvers, _type.type.bool, function () {
  return yup.bool();
}), _defineProperty(_resolvers, _type.type.object, function () {
  return yup.object();
}), _defineProperty(_resolvers, _type.type.date, function (typeErrorMessage) {
  return yup.date().typeError(typeErrorMessage || 'Value must be a date');
}), _defineProperty(_resolvers, _type.type.file, function () {
  return yup.mixed();
}), _resolvers);

var buildSubResolver = function buildSubResolver(props, key, dependencies, rawData) {
  var type = props.type,
      _props$constraints = props.constraints,
      constraints = _props$constraints === void 0 ? [] : _props$constraints;

  if (props.array || props.isMulti) {
    var subResolver;
    var arrayResolver = yup.array();

    if (props.schema && props.schema.type) {
      subResolver = buildSubResolver(props.schema, key, dependencies, rawData);
      arrayResolver = arrayResolver.of(subResolver);
    } else if (props.schema) {
      var deps = [];
      subResolver = yup.object().shape(getShapeAndDependencies(props.flow || Object.keys(props.schema), props.schema, deps, rawData).shape, deps);
      arrayResolver = arrayResolver.of(yup.object().shape({
        value: subResolver
      }));
    }

    return constraints.reduce(function (resolver, constraint) {
      return jsonOrFunctionConstraint(constraint, resolver, key, dependencies);
    }, arrayResolver);
  } else if (props.type === type.object && props.schema) {
    var _subResolver = getShapeAndDependencies(props.flow || Object.keys(props.schema), props.schema, dependencies, rawData);

    return constraints.reduce(function (resolver, constraint) {
      return jsonOrFunctionConstraint(constraint, resolver, key, dependencies);
    }, yup.object().shape(_subResolver.shape, dependencies));
  } else if (props.type === type.object && props.conditionalSchema) {
    var _option$map$getOrElse = (0, _Option.option)(props.conditionalSchema).map(function (condiSchema) {
      var ref = (0, _Option.option)(condiSchema.ref).map(function (ref) {
        return rawData[ref];
      }).getOrNull();
      var filterSwitch = condiSchema["switch"].find(function (s) {
        if (typeof s.condition === 'function') {
          return s.condition({
            rawData: rawData,
            ref: ref
          });
        } else {
          return s.condition === ref;
        }
      });
      return (0, _Option.option)(filterSwitch).getOrElse(condiSchema["switch"].find(function (s) {
        return s["default"];
      }));
    }).getOrElse({}),
        schema = _option$map$getOrElse.schema,
        flow = _option$map$getOrElse.flow;

    var _subResolver2 = getShapeAndDependencies(flow || Object.keys(schema), schema, dependencies, rawData);

    return constraints.reduce(function (resolver, constraint) {
      return jsonOrFunctionConstraint(constraint, resolver, key, dependencies);
    }, yup.object().shape(_subResolver2.shape, dependencies));
  } else {
    return constraints.reduce(function (resolver, constraint) {
      return jsonOrFunctionConstraint(constraint, resolver, key, dependencies);
    }, resolvers[type]());
  }
};

exports.buildSubResolver = buildSubResolver;

var jsonOrFunctionConstraint = function jsonOrFunctionConstraint(constraint, resolver, key, dependencies) {
  if (typeof constraint === 'function') {
    return constraint(resolver, key, dependencies);
  } else {
    return _constraints.jsonConstraints[constraint.type](constraint)(resolver, key, dependencies);
  }
};

var getShapeAndDependencies = function getShapeAndDependencies(flow, schema) {
  var dependencies = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
  var rawData = arguments.length > 3 ? arguments[3] : undefined;
  var shape = (flow || Object.keys(schema)).reduce(function (resolvers, key) {
    if (_typeof(key) === 'object') {
      return _objectSpread(_objectSpread({}, resolvers), getShapeAndDependencies(key.flow, schema, dependencies, rawData).shape);
    }

    var resolver = buildSubResolver(schema[key], key, dependencies, rawData);
    return _objectSpread(_objectSpread({}, resolvers), {}, _defineProperty({}, key, resolver));
  }, {});
  return {
    shape: shape,
    dependencies: dependencies
  };
};

exports.getShapeAndDependencies = getShapeAndDependencies;