"use strict";

exports.__esModule = true;
exports.default = void 0;

var _utils = require("../utils");

var _vnodes = require("../utils/vnodes");

var _createNamespace = (0, _utils.createNamespace)('form'),
    createComponent = _createNamespace[0],
    bem = _createNamespace[1];

var _default = createComponent({
  props: {
    colon: Boolean,
    disabled: Boolean,
    readonly: Boolean,
    labelWidth: [Number, String],
    labelAlign: String,
    inputAlign: String,
    scrollToError: Boolean,
    validateFirst: Boolean,
    errorMessageAlign: String,
    submitOnEnter: {
      type: Boolean,
      default: true
    },
    validateTrigger: {
      type: String,
      default: 'onBlur'
    },
    showError: {
      type: Boolean,
      default: true
    },
    showErrorMessage: {
      type: Boolean,
      default: true
    },

    /**** patch 部分【start】*****/
    noErrorPop: {
      type: Boolean,
      default: false
    },
    popScrollToError: {
      type: Boolean,
      default: true
    },
    showAllErrorMessage: {
      type: Boolean,
      default: false
    }
    /**** patch 部分【end】*****/

  },
  provide: function provide() {
    return {
      vanForm: this
    };
  },
  data: function data() {
    return {
      /**** patch 部分【start】*****/
      errorFieldName: '',

      /**** patch 部分【end】*****/
      fields: []
    };
  },

  /**** patch 部分【start】*****/
  created: function created() {
    this.$on('failed', this.setErrorFieldName);
  },

  /**** patch 部分【end】*****/

  /**** patch 部分【start】*****/
  mounted: function mounted() {
    var _this = this;

    if (!this.noErrorPop) {
      this.$nextTick(function () {
        setTimeout(function () {
          var className = '';

          if (_this.$el.className) {
            className = _this.$el.className + ' ';
          }

          _this.$el.className = className + 'ksui-form';
        }, 50);
      });
    }
  },

  /**** patch 部分【end】*****/

  /**** patch 部分【start】*****/
  beforeDestroy: function beforeDestroy() {
    this.$off('failed', this.setErrorFieldName);
  },

  /**** patch 部分【end】*****/
  methods: {
    /**** patch 部分【start】*****/
    setErrorFieldName: function setErrorFieldName(params) {
      if (!this.noErrorPop && params && Array.isArray(params.errors) && params.errors.length > 0) {
        this.errorFieldName = params.errors[0].name;

        if (this.popScrollToError) {
          this.scrollToField(this.errorFieldName);
        }
      }
    },
    failHandler: function failHandler(errors) {
      if (!Array.isArray(errors) || errors.length === 0) {
        return;
      }

      this.$emit('failed', {
        values: this.getValues(),
        errors: errors
      });

      if (this.popScrollToError) {
        this.scrollToField(errors[0].name);
      }
    },

    /**** patch 部分【end】*****/
    getFieldsByNames: function getFieldsByNames(names) {
      if (names) {
        return this.fields.filter(function (field) {
          return names.indexOf(field.name) !== -1;
        });
      }

      return this.fields;
    },
    validateSeq: function validateSeq(names) {
      var _this2 = this;

      return new Promise(function (resolve, reject) {
        var errors = [];

        var fields = _this2.getFieldsByNames(names);

        fields.reduce(function (promise, field) {
          return promise.then(function () {
            if (!errors.length) {
              return field.validate().then(function (error) {
                if (error) {
                  errors.push(error);
                }
              });
            }
          });
        }, Promise.resolve()).then(function () {
          if (errors.length) {
            reject(errors);
          } else {
            resolve();
          }
        });
      });
    },
    validateFields: function validateFields(names) {
      var _this3 = this;

      return new Promise(function (resolve, reject) {
        var fields = _this3.getFieldsByNames(names);

        Promise.all(fields.map(function (item) {
          return item.validate();
        })).then(function (errors) {
          errors = errors.filter(function (item) {
            return item;
          });

          if (errors.length) {
            reject(errors);
          } else {
            resolve();
          }
        });
      });
    },
    // @exposed-api
    validate: function validate(name) {
      if (name && !Array.isArray(name)) {
        return this.validateField(name);
      }

      return this.validateFirst ? this.validateSeq(name) : this.validateFields(name);
    },
    validateField: function validateField(name) {
      var matched = this.fields.filter(function (item) {
        return item.name === name;
      });

      if (matched.length) {
        return new Promise(function (resolve, reject) {
          matched[0].validate().then(function (error) {
            if (error) {
              reject(error);
            } else {
              resolve();
            }
          });
        });
      }

      return Promise.reject();
    },
    // @exposed-api
    resetValidation: function resetValidation(name) {
      if (name && !Array.isArray(name)) {
        name = [name];
      }

      var fields = this.getFieldsByNames(name);
      fields.forEach(function (item) {
        item.resetValidation();
      });
    },
    // @exposed-api
    scrollToField: function scrollToField(name, options) {
      this.fields.some(function (item) {
        if (item.name === name) {
          item.$el.scrollIntoView(options);
          return true;
        }

        return false;
      });
    },
    addField: function addField(field) {
      this.fields.push(field);
      (0, _vnodes.sortChildren)(this.fields, this);
    },
    removeField: function removeField(field) {
      this.fields = this.fields.filter(function (item) {
        return item !== field;
      });
    },
    getValues: function getValues() {
      return this.fields.reduce(function (form, field) {
        form[field.name] = field.formValue;
        return form;
      }, {});
    },
    onSubmit: function onSubmit(event) {
      event.preventDefault();
      this.submit();
    },
    // @exposed-api
    submit: function submit() {
      var _this4 = this;

      var values = this.getValues();
      this.validate().then(function () {
        _this4.$emit('submit', values);
      }).catch(function (errors) {
        _this4.$emit('failed', {
          values: values,
          errors: errors
        });

        if (_this4.scrollToError) {
          _this4.scrollToField(errors[0].name);
        }
      });
    }
  },
  render: function render() {
    var h = arguments[0];
    return h("form", {
      "class": bem(),
      "on": {
        "submit": this.onSubmit
      }
    }, [this.slots()]);
  }
});

exports.default = _default;