import { j as l } from "./index.es36.js";
import { Component as p } from "react";
import s from "./index.es38.js";
import "./index.es39.js";
import "./index.es40.js";
import "./index.es41.js";
import "./index.es42.js";
import "./index.es43.js";
import "./index.es44.js";
import "./index.es45.js";
import "./index.es46.js";
import "./index.es47.js";
import "./index.es48.js";
import "./index.es49.js";
import "./index.es50.js";
import "./index.es51.js";
import "./index.es52.js";
import "./index.es53.js";
import "./index.es54.js";
import "./index.es55.js";
import "./index.es56.js";
import "./index.es57.js";
import "./index.es58.js";
import "./index.es59.js";
import "./index.es60.js";
import "./index.es61.js";
import "./index.es62.js";
import "./index.es63.js";
import "./index.es64.js";
import "./index.es65.js";
import n from "./index.es14.js";
import h from "./index.es32.js";
const u = "can't be blank", C = "has already been taken", H = (d) => class extends p {
  /**
   * Constructs a new EditProvider component.
   *
   * @param props
   */
  constructor(t) {
    super(t);
    const i = s.defaults(t.item || {}, t.defaults || {});
    this.state = {
      item: i,
      loading: !1,
      originalItem: i,
      saving: !1,
      validationErrors: []
    };
  }
  /**
   * Loads the item from the API if an onInitialize prop is specified.
   */
  componentDidMount() {
    this.props.onInitialize && this.props.item && this.props.item.id && this.setState({ loading: !0 }, () => {
      this.props.onInitialize && this.props.item && this.props.onInitialize(this.props.item.id).then((t) => this.setState({ item: t, originalItem: t, loading: !1 }));
    });
  }
  /**
   * Resets the item on the state.
   *
   * @param prevProps
   */
  componentDidUpdate(t) {
    t.item !== this.props.item && this.setState({ item: this.props.item, originalItem: this.props.item });
  }
  /**
   * Sets the saving property to false.
   */
  componentWillUnmount() {
    this.onSetState({ saving: !1 });
  }
  /**
   * Returns true if the IDs or UIDs for the passed records match.
   *
   * @param a
   * @param b
   *
   * @returns {*|boolean}
   */
  isChild(t, i) {
    return t.uid && i.uid && t.uid === i.uid || t.id && i.id && t.id === i.id;
  }
  /**
   * Returns true if the passed property is required.
   *
   * @param prop
   *
   * @returns {*|boolean}
   */
  isRequired(t) {
    return this.props.required && s.contains(this.props.required, t);
  }
  /**
   * Returns true if the passed property is listed in the hash of validation errors.
   *
   * @param prop
   *
   * @returns {*|boolean}
   */
  isError(t) {
    return s.has(this.state.validationErrors, t);
  }
  /**
   * Clears the validation errors for the passed keys.
   *
   * @param keys
   */
  onClearValidationError(...t) {
    this.setState((i) => ({ validationErrors: s.omit(i.validationErrors, t) }));
  }
  /**
   * Closes the provider.
   */
  onClose() {
    this.props.onClose();
  }
  /**
   * Adds the passed child to the passed association for the current item.
   *
   * @param association
   * @param child
   */
  onCreateChildAssociation(t, i) {
    this.setState((e) => ({
      item: {
        ...e.item,
        [t]: [
          ...e.item[t] || [],
          i
        ]
      }
    }));
  }
  /**
   * Removes the passed child from the passed association.
   *
   * @param association
   * @param child
   */
  onDeleteChildAssociation(t, i) {
    return i.id ? this.onMarkChildAssociationForDelete(t, i) : this.onRemoveChildAssociation(t, i);
  }
  /**
   * Resolves the validation errors from the passed HTTP response.
   *
   * @param errors
   * @param status
   */
  onError({ response: { data: { errors: t = {} }, status: i } }) {
    const e = {};
    s.each(Object.keys(t), (o) => {
      const r = t[o], m = this.state.item[o];
      s.each(r, (a) => {
        a === C ? s.extend(e, { [o]: n.t("EditContainer.errors.unique", { key: o, value: m }) }) : a === u ? s.extend(e, { [o]: n.t("EditContainer.errors.required", { key: o }) }) : this.props.resolveValidationError && s.extend(e, this.props.resolveValidationError({
          key: o,
          error: a,
          status: i,
          item: this.state.item
        }));
      });
    }), i === 400 && s.isEmpty(e) ? s.extend(e, { error: n.t("EditContainer.errors.general") }) : i === 500 && s.isEmpty(e) && s.extend(e, { error: n.t("EditContainer.errors.system") }), this.setState({ saving: !1, validationErrors: e });
  }
  /**
   * Marks the passed child for delete form the passed association. This function is called if the child record
   * has an ID value.
   *
   * @param association
   * @param child
   */
  onMarkChildAssociationForDelete(t, i) {
    this.setState((e) => ({
      item: {
        ...e.item,
        [t]: s.map(
          e.item[t] || [],
          (o) => o.id === i.id ? { ...o, _destroy: !0 } : o
        )
      }
    }));
  }
  /**
   * Modifies the passed association based on the passed collection of children.
   *
   * @param association
   * @param children
   */
  onMultiAddChildAssociations(t, i) {
    const e = this.state.item[t];
    s.each(i, this.onSaveChildAssociation.bind(this, t));
    const o = s.filter(e, (r) => !s.find(i, this.isChild.bind(this, r)));
    s.each(o, this.onDeleteChildAssociation.bind(this, t));
  }
  /**
   * Removes the passed child record from the passed association. This function is called if the child record does
   * not have an ID value. If both objects have a "uid" property or "id" property, those attributes will be used
   * to compare. Otherwise, the isEqual function from ObjectUtils will be used to compare.
   *
   * @param association
   * @param child
   */
  onRemoveChildAssociation(t, i) {
    this.setState((e) => ({
      item: {
        ...e.item,
        [t]: s.filter(
          e.item[t] || [],
          (o) => !(this.isChild(o, i) || h.isEqual(o, i))
        )
      }
    }));
  }
  /**
   * Resets the item on the state to the default item and calls the onReset prop.
   */
  onReset() {
    const t = this.props.defaults || {};
    this.setState({
      item: t,
      originalItem: t
    });
  }
  /**
   * Saves the current item.
   */
  onSave() {
    this.validateForm() && this.setState({ saving: !0 }, () => {
      this.props.onSave(this.state.item).catch(this.onError.bind(this)).finally(() => this.setState({ saving: !1 }));
    });
  }
  /**
   * Adds the passed child record to the passed association.
   *
   * @param association
   * @param child
   */
  onSaveChildAssociation(t, i) {
    const e = this.state.item[t] || [];
    return s.find(e, this.isChild.bind(this, i)) ? this.onUpdateChildAssociation(t, i) : this.onCreateChildAssociation(t, i);
  }
  /**
   * Updates the child record in the passed association.
   *
   * @param association
   * @param child
   */
  onUpdateChildAssociation(t, i) {
    this.setState((e) => ({
      item: {
        ...e.item,
        [t]: s.map(e.item[t] || [], (o) => this.isChild(i, o) ? i : o)
      }
    }));
  }
  /**
   * Sets the associated ID and object.
   *
   * @param idKey
   * @param valueKey
   * @param record
   */
  onAssociationInputChange(t, i, e = {}) {
    this.setState((o) => ({
      item: {
        ...o.item,
        [t]: e.id || "",
        [i]: e || {}
      },
      validationErrors: s.omit(o.validationErrors, t)
    }));
  }
  /**
   * Toggles the checkbox value for the passed key.
   *
   * @param key
   */
  onCheckboxInputChange(t) {
    this.setState((i) => ({
      item: {
        ...i.item,
        [t]: !i.item[t]
      }
    }));
  }
  /**
   * Sets the passed properties on the item on the state.
   *
   * @param props
   */
  onSetState(t) {
    this.setState((i) => ({
      item: {
        ...i.item,
        ...t
      },
      validationErrors: s.omit(i.validationErrors, s.keys(t))
    }));
  }
  /**
   * Sets the text input property for the passed key/value.
   *
   * @param key
   * @param e
   * @param value
   */
  onTextInputChange(t, i, { value: e }) {
    this.setState((o) => ({
      item: {
        ...o.item,
        [t]: e
      },
      validationErrors: s.omit(o.validationErrors, t)
    }));
  }
  render() {
    return /* @__PURE__ */ l.jsx(
      d,
      {
        ...this.props,
        dirty: !!(this.state.item.id && !h.isEqual(this.state.item, this.state.originalItem)),
        errors: s.values(this.state.validationErrors),
        isError: this.isError.bind(this),
        isRequired: this.isRequired.bind(this),
        item: this.state.item,
        loading: this.state.loading,
        onAssociationInputChange: this.onAssociationInputChange.bind(this),
        onCheckboxInputChange: this.onCheckboxInputChange.bind(this),
        onClearValidationError: this.onClearValidationError.bind(this),
        onDeleteChildAssociation: this.onDeleteChildAssociation.bind(this),
        onMultiAddChildAssociations: this.onMultiAddChildAssociations.bind(this),
        onReset: this.onReset.bind(this),
        onSave: this.onSave.bind(this),
        onSaveChildAssociation: this.onSaveChildAssociation.bind(this),
        onTextInputChange: this.onTextInputChange.bind(this),
        onSetState: this.onSetState.bind(this),
        saving: this.state.saving
      }
    );
  }
  /**
   * Validates the form using the component props required attributes.
   *
   * @returns {boolean}
   */
  validateForm() {
    const t = [];
    this.props.validate && s.extend(t, this.props.validate(this.state.item));
    const i = this.props.required || [];
    return s.each(i, (e) => {
      const o = this.state.item[e];
      let r;
      s.isNumber(o) ? r = s.isEmpty(o.toString()) : r = s.isEmpty(o), r && s.extend(t, { [e]: n.t("EditContainer.errors.required", { key: e }) });
    }), this.setState({ validationErrors: t }), s.keys(t).length === 0;
  }
};
export {
  H as default
};
//# sourceMappingURL=index.es3.js.map
