import { ErrorHelper as s } from "./accelbyte-widgets120.mjs";
import { VALIDATION_ERROR_CODE as e } from "@accelbyte/sdk";
import { Container as a } from "unstated";
import { Iam as n } from "@accelbyte/sdk-iam";
const r = {
  isFetching: !1,
  fetchError: null,
  validations: [],
  version: 0
};
class u extends a {
  constructor(i) {
    super(), this.sdk = i, this.state = r;
  }
  async fetchValidations(i) {
    if (!this.state.isFetching)
      try {
        this.setState({ isFetching: this.state.validations.length === 0, fetchError: null });
        const t = await n.InputValidationsApi(this.sdk).getInputValidations({ languageCode: i });
        return this.setState({ validations: t.data, version: t.version }), t;
      } catch (t) {
        throw this.setState({ fetchError: t }), t;
      } finally {
        this.setState({ isFetching: !1 });
      }
  }
  async isVersionOutdated() {
    const i = this.state.version.valueOf(), t = await this.fetchValidations();
    return i !== (t == null ? void 0 : t.version);
  }
  isInputValidationError(i) {
    return s.isAxiosError(i) && s.extractServiceErrorCode(i) === e ? this.isVersionOutdated() : !1;
  }
  clear() {
    this.setState(r);
  }
}
export {
  u as InputValidationAppManager
};
