import { Container as h } from "unstated";
import { Legal as r, LegalHelper as g, LegalPolicyType as l } from "@accelbyte/sdk-legal";
const c = {
  isLoading: !1,
  eligibilities: null,
  policiesByCountry: null,
  isFetchingPolicy: !1,
  isAccepting: !1,
  acceptedAgreements: null,
  error: null,
  preferences: null,
  marketingPreferences: [],
  policyVersion: null,
  isUpdating: !1
};
class u extends h {
  constructor(t) {
    super(), this.sdk = t, this.fetchPoliciesList = async (e, i = !0) => {
      if (!this.state.isFetchingPolicy)
        try {
          this.setState({ isFetchingPolicy: !0, error: null });
          const s = await r.PoliciesApi(this.sdk).getPolicyCountry_ByCountryCode_ByNS(e, { defaultOnEmpty: i });
          return this.setState({ policiesByCountry: s }), s;
        } catch (s) {
          throw this.setState({ error: s }), s;
        } finally {
          this.setState({ isFetchingPolicy: !1 });
        }
    }, this.bulkAcceptPolicies = async (e, i) => {
      if (!this.state.isAccepting)
        try {
          this.setState({ isAccepting: !0, error: null }), await r.AgreementApi(this.sdk).createAgreementPolicy(e), i && i();
        } catch (s) {
          throw this.setState({ error: s }), s;
        } finally {
          this.setState({ isAccepting: !1 });
        }
    }, this.clear = () => {
      this.setState(c);
    }, this.state = c;
  }
  async fetchUnsignedPolicies() {
    if (!this.state.isLoading)
      try {
        this.setState({
          isLoading: !0,
          eligibilities: null,
          error: null
        });
        const t = await r.EligibilitiesApi(this.sdk).getEligibility_ByNamespace(), e = g.getUnsignedPolicies(t);
        return this.setState({ eligibilities: e }), e;
      } catch (t) {
        throw this.setState({ error: t }), t;
      } finally {
        this.setState({ isLoading: !1 });
      }
  }
  async fetchMarketingPreferences() {
    var t;
    if (!this.state.isLoading)
      try {
        this.setState({
          isLoading: !0,
          preferences: null,
          error: null
        });
        const e = await r.EligibilitiesApi(this.sdk).getEligibility_ByNamespace(), i = [];
        for (const s of e)
          if (s.policyType === l.MARKETING_PREFERENCE) {
            let a = s.policyVersions || [];
            if (a = a.slice(0, 1), (t = a[0]) != null && t.localizedPolicyVersions) {
              const n = a[0].localizedPolicyVersions.filter((o) => o.isDefaultSelection);
              a[0].localizedPolicyVersions = n, n.length > 0 && i.push(s);
            }
          }
        return this.setState({ preferences: i }), i;
      } catch (e) {
        throw this.setState({ error: e }), e;
      } finally {
        this.setState({ isLoading: !1 });
      }
  }
  async updateMarketingPreferences(t) {
    if (!this.state.isUpdating)
      try {
        this.setState({
          isUpdating: !0,
          error: null
        }), await r.AgreementApi(this.sdk).patchAgreementLocalizedPolicyVersionPreference(t);
      } catch (e) {
        throw this.setState({ error: e }), e;
      } finally {
        this.setState({ isUpdating: !1 });
      }
  }
  async fetchMarketingPreferenceRecord() {
    if (!this.state.isLoading)
      try {
        this.setState({
          isLoading: !0,
          marketingPreferences: [],
          error: null
        });
        const e = (await r.AgreementApi(this.sdk).getAgreementsPolicies()).filter((i) => i.policyType === l.MARKETING_PREFERENCE);
        if (!e)
          return;
        this.setState({ marketingPreferences: e }, async () => {
          await Promise.all([
            e.map((i) => this.fetchMarketingPreferenceLocalizedVersion(i))
          ]);
        });
      } catch (t) {
        throw this.setState({ error: t }), t;
      } finally {
        this.setState({ isLoading: !1 });
      }
  }
  async fetchMarketingPreferenceLocalizedVersion(t) {
    if (!(this.state.isLoading || !t.localizedPolicyVersion))
      try {
        this.setState({
          isLoading: !0,
          error: null
        });
        const e = await r.LocalizedPolicyVersionsApi(this.sdk).getLocalizedPolicyVersion_ByLocalizedPolicyVersionId(
          t.localizedPolicyVersion.id
        );
        this.setState((i) => ({
          marketingPreferences: i.marketingPreferences.map((s) => s.id === t.id ? Object.assign(s, { policyVersion: e.policyVersion }) : s)
        }));
      } catch (e) {
        throw this.setState({ error: e }), e;
      } finally {
        this.setState({ isLoading: !1 });
      }
  }
  async fetchAcceptedAgreement() {
    if (!this.state.isLoading)
      try {
        this.setState({
          isLoading: !0,
          acceptedAgreements: null,
          error: null
        });
        const e = (await r.AgreementApi(this.sdk).getAgreementsPolicies()).filter((i) => i.isAccepted);
        return this.setState({ acceptedAgreements: e }), e;
      } catch (t) {
        throw this.setState({ error: t }), t;
      } finally {
        this.setState({ isLoading: !1 });
      }
  }
}
export {
  u as LegalAppManager
};
