import _Object$getOwnPropertyDescriptor from "@babel/runtime-corejs2/core-js/object/get-own-property-descriptor";
import "core-js/modules/es6.number.constructor";
import "core-js/modules/es6.array.find";
import _Promise from "@babel/runtime-corejs2/core-js/promise";
import "core-js/modules/web.dom.iterable";
import "core-js/modules/es6.array.iterator";
import "core-js/modules/es6.string.iterator";
import _inheritsLoose from "../../inheritsLoose";
import _defineProperty from "@babel/runtime-corejs2/helpers/defineProperty";
import _applyDecoratedDescriptor from "@babel/runtime-corejs2/helpers/applyDecoratedDescriptor";

var _dec, _class, _init, _class2, _temp;

import Intact from 'intact-angular';
import template from './form.vdt';
import { methods, messages, classNames, addMethod } from './methods';
import '../../styles/kpc.css';
import './index.css';
var Form = (_dec = Intact.template(), (_class = (_temp = _class2 =
/*#__PURE__*/
function (_Intact) {
  _inheritsLoose(Form, _Intact);

  function Form() {
    return _Intact.apply(this, arguments) || this;
  }

  var _proto = Form.prototype;

  _proto.defaults = function defaults() {
    return {
      items: [],
      rules: {},
      labelWidth: undefined,
      layout: 'horizontal',
      starOnRequired: false
    };
  } // getRules() {
  // const rules = this.get('rules');
  // const items = this.get('items');
  // items.forEach(item => {
  // rules[item.get('model')] = item.get('rules');
  // });
  // return rules;
  // }
  ;

  _proto.validate = function validate() {
    var items = this.get('items');
    return _Promise.all(items.map(function (item) {
      return item.validate();
    })).then(function (values) {
      return values.every(function (value) {
        return value;
      });
    });
  };

  _proto.reset = function reset() {
    var items = this.get('items');
    items.forEach(function (item) {
      return item.reset();
    });
  };

  _proto.getItem = function getItem(model) {
    var items = this.get('items');
    /* istanbul ignore if */

    if (model === undefined) return items;
    return items.find(function (item) {
      return item.get('model') === model;
    });
  } // optional(item) {
  // const value = item.get('value');
  // return !Form.methods.required.call(this, value, item);
  // }
  ;

  _proto.getFirstInvalidFormItem = function getFirstInvalidFormItem() {
    return this.get('items').find(function (item) {
      if (item.get('isValid') === false) {
        return item;
      }
    });
  };

  _proto._submit = function _submit(e) {
    e.preventDefault();
    this.submit(e);
  };

  _proto.submit = function submit(e) {
    var _this = this;

    this.validate().then(function (isValid) {
      if (isValid) {
        _this.trigger('submit', e, _this);
      }
    });
  };

  return Form;
}(Intact), _defineProperty(_class2, "methods", methods), _defineProperty(_class2, "messages", messages), _defineProperty(_class2, "classNames", classNames), _defineProperty(_class2, "addMethod", addMethod), _defineProperty(_class2, "template", template), _defineProperty(_class2, "propTypes", {
  items: Array,
  rules: Object,
  labelWidth: [String, Number],
  layout: ['horizontal', 'vertical', 'inline'],
  starOnRequired: Boolean
}), _defineProperty(_class2, "events", {
  submit: true
}), _temp), (_applyDecoratedDescriptor(_class, "template", [_dec], (_init = _Object$getOwnPropertyDescriptor(_class, "template"), _init = _init ? _init.value : undefined, {
  enumerable: true,
  configurable: true,
  writable: true,
  initializer: function initializer() {
    return _init;
  }
}), _class)), _class));
export { Form as default };