import get_1 from "../../../../../../lodash@4.17.21/node_modules/lodash/get.js";
import setWith_1 from "../../../../../../lodash@4.17.21/node_modules/lodash/setWith.js";
import has_1 from "../../../../../../lodash@4.17.21/node_modules/lodash/has.js";
import omit_1 from "../../../../../../lodash@4.17.21/node_modules/lodash/omit.js";
import { iterativelyGetKeys, cloneDeep, set } from "./utils.js";
import { isObject, isArray, isString } from "../_util/is.js";
import promisify from "./promisify.js";
var __assign = globalThis && globalThis.__assign || function() {
  __assign = Object.assign || function(t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];
      for (var p in s)
        if (Object.prototype.hasOwnProperty.call(s, p))
          t[p] = s[p];
    }
    return t;
  };
  return __assign.apply(this, arguments);
};
var __read = globalThis && globalThis.__read || function(o, n) {
  var m = typeof Symbol === "function" && o[Symbol.iterator];
  if (!m)
    return o;
  var i = m.call(o), r, ar = [], e;
  try {
    while ((n === void 0 || n-- > 0) && !(r = i.next()).done)
      ar.push(r.value);
  } catch (error) {
    e = { error };
  } finally {
    try {
      if (r && !r.done && (m = i["return"]))
        m.call(i);
    } finally {
      if (e)
        throw e.error;
    }
  }
  return ar;
};
var __spreadArray = globalThis && globalThis.__spreadArray || function(to, from, pack) {
  if (pack || arguments.length === 2)
    for (var i = 0, l = from.length, ar; i < l; i++) {
      if (ar || !(i in from)) {
        if (!ar)
          ar = Array.prototype.slice.call(from, 0, i);
        ar[i] = from[i];
      }
    }
  return to.concat(ar || Array.prototype.slice.call(from));
};
var Store = function() {
  function Store2() {
    var _this = this;
    this.registerFields = [];
    this.registerWatchers = [];
    this.touchedFields = {};
    this.store = {};
    this.initialValues = {};
    this.callbacks = {};
    this.innerSetCallbacks = function(values) {
      _this.callbacks = values;
    };
    this.registerWatcher = function(item) {
      _this.registerWatchers.push(item);
      return function() {
        _this.registerWatchers = _this.registerWatchers.filter(function(x) {
          return x !== item;
        });
      };
    };
    this.registerField = function(item) {
      _this.registerFields.push(item);
      _this.notifyWatchers();
      return function() {
        _this.registerFields = _this.registerFields.filter(function(x) {
          return x !== item;
        });
        _this.notifyWatchers();
      };
    };
    this.getRegisteredFields = function(hasField) {
      if (hasField) {
        return _this.registerFields.filter(function(control) {
          var _a;
          return control.hasFieldProps() && !((_a = control.props) === null || _a === void 0 ? void 0 : _a.isFormList);
        });
      }
      return _this.registerFields;
    };
    this.getRegisteredField = function(field) {
      return _this.registerFields.filter(function(x) {
        return x.props.field === field;
      })[0];
    };
    this.notify = function(type, info) {
      if (type === "setFieldValue" || type === "innerSetValue" && !info.ignore) {
        _this._pushTouchField(info.changeValues ? iterativelyGetKeys(info.changeValues) : _this._getIterativelyKeysByField(info.field));
      }
      _this.registerFields.forEach(function(item) {
        item.onStoreChange && item.onStoreChange(type, __assign(__assign({}, info), { current: _this.store }));
      });
    };
    this.innerSetInitialValues = function(values) {
      if (!values)
        return;
      _this.initialValues = cloneDeep(values);
      Object.keys(values).forEach(function(field) {
        set(_this.store, field, values[field]);
      });
    };
    this.innerSetInitialValue = function(field, value) {
      if (!field)
        return;
      _this.initialValues[field] = value;
      if (!_this._inTouchFields(field)) {
        set(_this.store, field, get_1(_this.initialValues, field));
      }
    };
    this.innerSetFieldValue = function(field, value, options) {
      var _a, _b, _c;
      if (!field)
        return;
      var prev = cloneDeep(_this.store);
      set(_this.store, field, value);
      _this.triggerValuesChange((_a = {}, _a[field] = value, _a));
      _this.triggerTouchChange((_b = {}, _b[field] = value, _b));
      _this.notify("innerSetValue", __assign(__assign({ prev, field }, options), { changeValues: (_c = {}, _c[field] = value, _c) }));
    };
    this.innerGetStore = function() {
      return _this.store;
    };
    this.getTouchedFields = function() {
      return _this.getRegisteredFields(true).filter(function(item) {
        return item.isTouched();
      }).map(function(x) {
        return x.props.field;
      });
    };
    this.setFieldValue = function(field, value) {
      var _a;
      if (!field)
        return;
      _this.setFields((_a = {}, _a[field] = { value }, _a));
    };
    this.setFieldsValue = function(values) {
      if (isObject(values)) {
        var fields = Object.keys(values);
        var obj_1 = {};
        fields.forEach(function(field) {
          obj_1[field] = {
            value: values[field]
          };
        });
        _this.setFields(obj_1);
      }
    };
    this.setFields = function(obj) {
      var fields = Object.keys(obj);
      var changeValues = {};
      fields.forEach(function(field) {
        var _a;
        var item = obj[field];
        var prev = cloneDeep(_this.store);
        if (item) {
          var info = {};
          if ("error" in item) {
            info.errors = item.error;
          }
          if ("warning" in item) {
            info.warnings = item.warning;
          }
          if ("touched" in item) {
            info.touched = item.touched;
          }
          if ("value" in item) {
            set(_this.store, field, item.value);
            changeValues[field] = item.value;
          }
          _this.notify("setFieldValue", {
            data: info,
            prev,
            field,
            changeValues: (_a = {}, _a[field] = item.value, _a)
          });
        }
      });
      _this.triggerValuesChange(changeValues);
    };
    this.getFieldValue = function(field) {
      return cloneDeep(get_1(_this.store, field));
    };
    this.getFieldError = function(field) {
      var item = _this.getRegisteredField(field);
      return item ? item.getErrors() : null;
    };
    this.getFieldsError = function(fields) {
      var errors = {};
      if (isArray(fields)) {
        fields.map(function(field) {
          var error = _this.getFieldError(field);
          if (error) {
            errors[field] = error;
          }
        });
      } else {
        _this.getRegisteredFields(true).forEach(function(item) {
          if (item.getErrors()) {
            errors[item.props.field] = item.getErrors();
          }
        });
      }
      return errors;
    };
    this.getFields = function() {
      return cloneDeep(_this.store);
    };
    this.getFieldsValue = function(fields) {
      var values = {};
      if (isArray(fields)) {
        fields.forEach(function(key) {
          set(values, key, _this.getFieldValue(key));
        });
        return values;
      }
      _this.getRegisteredFields(true).forEach(function(_a) {
        var field = _a.props.field;
        var value = get_1(_this.store, field);
        set(values, field, value);
      });
      return values;
    };
    this.resetFields = function(fieldKeys) {
      var prev = cloneDeep(_this.store);
      var fields = isString(fieldKeys) ? [fieldKeys] : fieldKeys;
      if (fields && isArray(fields)) {
        var changeValues_1 = {};
        fields.forEach(function(field) {
          set(_this.store, field, _this.initialValues[field]);
          changeValues_1[field] = get_1(_this.store, field);
        });
        _this.triggerValuesChange(changeValues_1);
        _this.notify("reset", { prev, field: fields });
        _this._popTouchField(fields);
      } else {
        var newValues_1 = {};
        var changeValues_2 = cloneDeep(_this.store);
        Object.keys(_this.initialValues).forEach(function(field) {
          set(newValues_1, field, _this.initialValues[field]);
        });
        _this.store = newValues_1;
        _this.getRegisteredFields(true).forEach(function(item) {
          var key = item.props.field;
          set(changeValues_2, key, get_1(_this.store, key));
        });
        _this.triggerValuesChange(changeValues_2);
        _this._popTouchField();
        _this.notify("reset", { prev, field: Object.keys(changeValues_2) });
      }
    };
    this.validate = promisify(function(fieldsOrCallback, cb) {
      var callback = function() {
      };
      var controlItems = _this.getRegisteredFields(true);
      if (isArray(fieldsOrCallback) && fieldsOrCallback.length > 0) {
        controlItems = controlItems.filter(function(x) {
          return fieldsOrCallback.indexOf(x.props.field) > -1;
        });
        callback = cb || callback;
      } else if (typeof fieldsOrCallback === "function") {
        callback = fieldsOrCallback;
      }
      var promises = controlItems.map(function(x) {
        return x.validateField();
      });
      Promise.all(promises).then(function(result) {
        var errors = {};
        var values = {};
        result.map(function(x) {
          if (x.error) {
            errors = __assign(__assign({}, errors), x.error);
          }
          set(values, x.field, x.value);
        });
        if (Object.keys(errors).length) {
          var onValidateFail = _this.callbacks.onValidateFail;
          onValidateFail && onValidateFail(errors);
          callback && callback(errors, cloneDeep(values));
        } else {
          callback && callback(null, cloneDeep(values));
        }
      });
    });
    this.submit = function() {
      _this.validate(function(errors, values) {
        if (!errors) {
          var onSubmit = _this.callbacks.onSubmit;
          onSubmit && onSubmit(values);
        } else {
          var onSubmitFailed = _this.callbacks.onSubmitFailed;
          onSubmitFailed && onSubmitFailed(errors);
        }
      });
    };
    this.clearFields = function(fieldKeys) {
      var prev = cloneDeep(_this.store);
      var fields = isString(fieldKeys) ? [fieldKeys] : fieldKeys;
      if (fields && isArray(fields)) {
        var changeValues_3 = {};
        fields.forEach(function(field) {
          set(_this.store, field, void 0);
          changeValues_3[field] = get_1(_this.store, field);
        });
        _this.triggerValuesChange(changeValues_3);
        _this.notify("setFieldValue", {
          prev,
          field: fields,
          data: {
            errors: null,
            warnings: null
          }
        });
      } else {
        var changeValues_4 = {};
        _this.store = {};
        _this.getRegisteredFields(true).forEach(function(item) {
          var key = item.props.field;
          set(changeValues_4, key, void 0);
        });
        _this.triggerValuesChange(changeValues_4);
        _this.notify("setFieldValue", {
          prev,
          field: Object.keys(changeValues_4),
          data: {
            errors: null,
            warnings: null
          }
        });
      }
    };
  }
  Store2.prototype.notifyWatchers = function() {
    this.registerWatchers.forEach(function(item) {
      item();
    });
  };
  Store2.prototype.triggerValuesChange = function(value) {
    if (value && Object.keys(value).length) {
      var onValuesChange = this.callbacks.onValuesChange;
      onValuesChange && onValuesChange(value, this.getFields());
    }
    this.notifyWatchers();
  };
  Store2.prototype.triggerTouchChange = function(value) {
    if (value && Object.keys(value).length) {
      var onChange = this.callbacks.onChange;
      onChange && onChange(value, this.getFields());
    }
  };
  Store2.prototype._getIterativelyKeysByField = function(field) {
    if (!field) {
      return [];
    }
    var fields = [].concat(field);
    var keys = fields.map(function(item) {
      return iterativelyGetKeys(set({}, item, void 0));
    }).reduce(function(total, next) {
      return total.concat(next);
    }, []);
    return __spreadArray([field], __read(keys), false);
  };
  Store2.prototype._inTouchFields = function(field) {
    var _this = this;
    var keys = this._getIterativelyKeysByField(field);
    return keys.some(function(item) {
      return has_1(_this.touchedFields, item);
    });
  };
  Store2.prototype._popTouchField = function(field) {
    if (field === void 0) {
      this.touchedFields = {};
    }
    var keys = this._getIterativelyKeysByField(field);
    this.touchedFields = omit_1(this.touchedFields, keys);
  };
  Store2.prototype._pushTouchField = function(field) {
    var _this = this;
    [].concat(field).forEach(function(key) {
      setWith_1(_this.touchedFields, key, void 0, Object);
    });
  };
  return Store2;
}();
const Store$1 = Store;
export {
  Store$1 as default
};
//# sourceMappingURL=store.js.map
