'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _rollupPluginBabelHelpers = require('./_virtual/_rollupPluginBabelHelpers.js');
var zod = require('zod');

var keys = ['required', 'minLength', 'maxLength', 'min', 'max', 'step', 'multiple', 'pattern'];
function getZodConstraint(schema) {
  function updateConstraint(schema, data) {
    var _data$name;
    var name = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
    var constraint = name !== '' ? (_data$name = data[name]) !== null && _data$name !== void 0 ? _data$name : data[name] = {
      required: true
    } : {};
    if (schema instanceof zod.ZodObject) {
      for (var key in schema.shape) {
        updateConstraint(schema.shape[key], data, name ? "".concat(name, ".").concat(key) : key);
      }
    } else if (schema instanceof zod.ZodEffects) {
      updateConstraint(schema.innerType(), data, name);
    } else if (schema instanceof zod.ZodPipeline) {
      // FIXME: What to do with .pipe()?
      updateConstraint(schema._def.out, data, name);
    } else if (schema instanceof zod.ZodIntersection) {
      var leftResult = {};
      var rightResult = {};
      updateConstraint(schema._def.left, leftResult, name);
      updateConstraint(schema._def.right, rightResult, name);
      Object.assign(data, leftResult, rightResult);
    } else if (schema instanceof zod.ZodUnion || schema instanceof zod.ZodDiscriminatedUnion) {
      Object.assign(data, schema.options.map(option => {
        var result = {};
        updateConstraint(option, result, name);
        return result;
      }).reduce((prev, next) => {
        var list = new Set([...Object.keys(prev), ...Object.keys(next)]);
        var result = {};
        for (var _name of list) {
          var prevConstraint = prev[_name];
          var nextConstraint = next[_name];
          if (prevConstraint && nextConstraint) {
            var _constraint = {};
            result[_name] = _constraint;
            for (var _key of keys) {
              if (typeof prevConstraint[_key] !== 'undefined' && typeof nextConstraint[_key] !== 'undefined' && prevConstraint[_key] === nextConstraint[_key]) {
                // @ts-expect-error Both are on the same type
                _constraint[_key] = prevConstraint[_key];
              }
            }
          } else {
            result[_name] = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, prevConstraint), nextConstraint), {}, {
              required: false
            });
          }
        }
        return result;
      }));
    } else if (name === '') {
      // All the cases below are not allowed on root
      throw new Error('Unsupported schema');
    } else if (schema instanceof zod.ZodArray) {
      constraint.multiple = true;
      updateConstraint(schema.element, data, "".concat(name, "[]"));
    } else if (schema instanceof zod.ZodString) {
      if (schema.minLength !== null) {
        constraint.minLength = schema.minLength;
      }
      if (schema.maxLength !== null) {
        constraint.maxLength = schema.maxLength;
      }
    } else if (schema instanceof zod.ZodOptional) {
      constraint.required = false;
      updateConstraint(schema.unwrap(), data, name);
    } else if (schema instanceof zod.ZodDefault) {
      constraint.required = false;
      updateConstraint(schema.removeDefault(), data, name);
    } else if (schema instanceof zod.ZodNumber) {
      if (schema.minValue !== null) {
        constraint.min = schema.minValue;
      }
      if (schema.maxValue !== null) {
        constraint.max = schema.maxValue;
      }
    } else if (schema instanceof zod.ZodEnum) {
      constraint.pattern = schema.options.map(option =>
      // To escape unsafe characters on regex
      option.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d')).join('|');
    } else if (schema instanceof zod.ZodTuple) {
      for (var i = 0; i < schema.items.length; i++) {
        updateConstraint(schema.items[i], data, "".concat(name, "[").concat(i, "]"));
      }
    } else ;
  }
  var result = {};
  updateConstraint(schema, result);
  return result;
}

exports.getZodConstraint = getZodConstraint;
