import _ from "lodash"
import commonDeclare from "./commonDeclare"
import commonApi from "./commonApi"
export default {
  mixins: [commonApi,commonDeclare],
  data: function () {
    return {
    }
  },
  created: function () {},
  mounted() {
    this.ruleWatcher()
    this.ruleCheck()
  },
  methods: {
    ruleWatcher() {
      if(!this.defaultRules)
        return
      const that = this;
      Object.keys(that.defaultRules).forEach(key => {
        that.$watch("model." + key, _.debounce(that.defaultRules[key], 1000))
      })
    },
    ruleCheck() {
      if(!this.pattern)
        return
      const keySet = new Set(Object.keys(this.pattern).concat(Object.keys(this.checkRules)))
      const res = {};
      for (let key of keySet.keys()) {
        const rule = []
        if(this.pattern[key]){
          rule.push(...this.pattern[key])
        } 
        if(this.checkRules[key]){
          for (let j = 0; j < this.checkRules[key].length; j++) {
            const check = this.checkRules[key][j];
            rule.push({
              validator: check.bind(this)
            })
          }
        }
        if(rule.length > 0) {
          res[key] = rule;
        }
      }
      this.rules = res;
    }
  }
}