/*! bxs-vue-ui v3.3.0 */
import '../utils/entry-virtual.js';
import { Q as _Promise, F as _concatInstanceProperty, C as create, _ as __vue_normalize__ } from '../utils/normalize-component.js';
import '../utils/html.js';
import '../utils/es.object.to-string.js';
import '../utils/array-iteration.js';
import '../utils/sloppy-array-method.js';
import { _ as _forEachInstanceProperty } from '../utils/for-each.js';
import { _ as _mapInstanceProperty } from '../utils/map.js';
import { _ as _filterInstanceProperty } from '../utils/filter.js';
import { _ as _Array$isArray } from '../utils/is-array.js';
import '../utils/extends.js';
import Vue from 'vue';
import '../icon/index.js';
import '../utils/splice.js';
import '../utils/a-function.js';
import '../utils/species-constructor.js';
import scrollIntoView from 'scroll-into-view-if-needed';
import '../utils/es.promise.js';
import '../utils/set-to-string-tag.js';
import { _ as _reduceInstanceProperty } from '../utils/reduce.js';
import '../utils/set-timeout.js';
import Toast from '../toast/index.js';

var promiseSequential = function promiseSequential(promises) {
  if (!_Array$isArray(promises)) {
    throw new Error('First argument need to be an array of Promises');
  }

  return new _Promise(function (resolve, reject) {
    var count = 0;
    var results = [];

    var iterateeFunc = function iterateeFunc(previousPromise, currentPromise) {
      return previousPromise.then(function (result) {
        if (count++ !== 0) results = _concatInstanceProperty(results).call(results, result);
        return currentPromise(result, results, count);
      }).catch(function (err) {
        return reject(err);
      });
    };

    promises = _concatInstanceProperty(promises).call(promises, function () {
      return _Promise.resolve();
    });

    _reduceInstanceProperty(promises).call(promises, iterateeFunc, _Promise.resolve(false)).then(function (res) {
      resolve(results);
    });
  });
};

var script = create({
  name: 'form',
  provide: function provide() {
    return {
      bxsForm: this
    };
  },
  props: {
    model: Object,
    rules: Object,
    // 可选，是否显示校验错误信息
    showMessage: {
      type: Boolean,
      default: true
    },
    // 可选，是否自动定位至错误项
    autoPositionError: {
      type: Boolean,
      default: true
    },
    // 可选，是否自动显示Toast提示
    autoShowToast: {
      type: Boolean,
      default: true
    },
    // 可选，默认Toast提示文案
    toastMessage: {
      type: String,
      default: '请完善填写内容'
    },
    // 可选，是否忽略最后一项的分割线
    ignoreLastLine: {
      type: Boolean,
      default: true
    },
    // 可选，是否所有内容垂直对齐
    isContentVerticalAlignment: {
      type: Boolean,
      default: true
    }
  },
  data: function data() {
    return {
      fields: []
    };
  },
  watch: {
    isContentVerticalAlignment: {
      handler: function handler() {
        this.$nextTick(this.reCalculateLabelWidth);
      },
      immediate: true
    }
  },
  created: function created() {
    var _this = this;

    this.$on('form.addField', function (field) {
      _this.fields.push(field);
    });
  },
  methods: {
    getFieldByProp: function getFieldByProp(prop) {
      var _context;

      var fields = _filterInstanceProperty(_context = this.fields).call(_context, function (field) {
        return field.prop === prop;
      });

      if (fields && fields.length > 0) {
        return fields[0];
      }

      return {};
    },
    validate: function validate(prop) {
      var _this2 = this;

      if (prop) {
        var field = this.getFieldByProp(prop);
        return field.validate();
      }

      return new _Promise(function (resolve, reject) {
        var _context2;

        promiseSequential(_mapInstanceProperty(_context2 = _this2.fields).call(_context2, function (field) {
          return function () {
            return field.validate();
          };
        })).then(function () {
          resolve();
        }).catch(function (_ref) {
          var errors = _ref.errors;

          if (_this2.autoPositionError) {
            _this2.readyPositionErrorField(errors[0].field);
          }

          if (_this2.autoShowToast) {
            Toast(_this2.toastMessage);
          }

          reject(errors);
        });
      });
    },
    // 支持多个Form表单定位
    readyPositionErrorField: function readyPositionErrorField(prop) {
      var _this3 = this;

      if (!Vue.bxsFormErrorFields) {
        Vue.bxsFormErrorFields = [];
      }

      Vue.bxsFormErrorFields.push(prop); // 增加macroTask(setTimeout)，保证在microTask(PromiseSequential)后执行

      window.setTimeout(function () {
        _this3.autoPositionErrorField(Vue.bxsFormErrorFields[0]);

        Vue.bxsFormErrorFields = [];
      });
    },
    // 自动定位到错误项
    autoPositionErrorField: function autoPositionErrorField(prop) {
      var _this$getFieldByProp = this.getFieldByProp(prop),
          $el = _this$getFieldByProp.$el;

      if ($el) {
        scrollIntoView($el, {
          behavior: 'smooth',
          scrollMode: 'if-needed'
        });
      }
    },
    // 重新计算每一项label的宽度
    reCalculateLabelWidth: function reCalculateLabelWidth() {
      if (this.isContentVerticalAlignment) {
        var _context3;

        var fields = _filterInstanceProperty(_context3 = this.fields).call(_context3, function (field) {
          return field.label && field.isLabelMoreThanThreshold;
        });

        if (fields.length > 0) {
          var _context4;

          /* eslint no-param-reassign: ["error", { "props": false }] */
          _forEachInstanceProperty(_context4 = this.fields).call(_context4, function (field) {
            field.isVerticalAlignmentForced = true;
          });
        }
      } else {
        var _context5;

        /* eslint no-param-reassign: ["error", { "props": false }] */
        _forEachInstanceProperty(_context5 = this.fields).call(_context5, function (field) {
          field.isVerticalAlignmentForced = false;
        });
      }
    },
    resetFields: function resetFields(prop) {
      if (prop) {
        var field = this.getFieldByProp(prop);
        field.resetField();
      } else {
        var _context6;

        _forEachInstanceProperty(_context6 = this.fields).call(_context6, function (field) {
          field.resetField();
        });
      }
    }
  }
});

/* script */
var __vue_script__ = script;
/* template */

var __vue_render__ = function __vue_render__() {
  var _vm = this;

  var _h = _vm.$createElement;

  var _c = _vm._self._c || _h;

  return _c('form', {
    staticClass: "bx-form",
    class: _vm.ignoreLastLine && 'bx-form--ignoredline'
  }, [_vm._t("default")], 2);
};

var __vue_staticRenderFns__ = [];
/* style */

var __vue_inject_styles__ = undefined;
/* scoped */

var __vue_scope_id__ = undefined;
/* module identifier */

var __vue_module_identifier__ = undefined;
/* functional template */

var __vue_is_functional_template__ = false;
/* style inject */

/* style inject SSR */

var index = __vue_normalize__({
  render: __vue_render__,
  staticRenderFns: __vue_staticRenderFns__
}, __vue_inject_styles__, __vue_script__, __vue_scope_id__, __vue_is_functional_template__, __vue_module_identifier__, undefined, undefined);

export default index;
