import { Alert as o } from "./accelbyte-widgets123.mjs";
import { Button as l } from "./accelbyte-widgets107.mjs";
import { CommonError as m } from "./accelbyte-widgets124.mjs";
import { Checkbox as p } from "./accelbyte-widgets335.mjs";
import { Loading as h } from "./accelbyte-widgets136.mjs";
import { Ctx as y } from "./accelbyte-widgets33.mjs";
import { AlertType as a } from "./accelbyte-widgets128.mjs";
import t from "react";
import { Trans as n } from "react-i18next";
import { AccountMainSection as d, AccountMainSectionHeader as f, AccountMainSectionBody as u } from "./accelbyte-widgets122.mjs";
import "./accelbyte-widgets336.mjs";
class v extends t.Component {
  constructor(i) {
    super(i), this.fetchMarketingPreferences = async () => {
      try {
        await Promise.all([this.logic.fetchMarketingPreferences(), this.logic.fetchMarketingPreferenceRecord()]);
      } catch (e) {
        this.setErrorAlert(e);
      }
    }, this.onAcceptSuccess = async () => {
      await this.fetchMarketingPreferences(), this.setPolicyMapFromLogic(), this.notifyAcceptSuccess();
    }, this.setErrorAlert = (e) => {
      this.setState({
        alert: {
          type: a.danger,
          element: /* @__PURE__ */ t.createElement(m, { error: e })
        }
      });
    }, this.notifyAcceptSuccess = () => {
      this.setState({
        alert: {
          type: a.success,
          element: /* @__PURE__ */ t.createElement(n, { i18nKey: "NotificationPreferencesPage.acceptSuccess" }, "Your notification preferences have been successfully updated")
        }
      });
    }, this.handleSubmit = async () => {
      const e = [], { policyMap: c } = this.state, { marketingPreferences: r } = this.logic.state;
      r && r.forEach((s) => {
        !s.policyId || !s.policyVersion || !s.localizedPolicyVersion || e.push({
          policyId: s.policyId,
          policyVersionId: s.policyVersion.id,
          localizedPolicyVersionId: s.localizedPolicyVersion.id,
          isAccepted: !!c.get(s.policyId)
        });
      }), this.logic.updateMarketingPreferences(e).then(() => {
        this.onAcceptSuccess();
      }).catch((s) => {
        this.setErrorAlert(s);
      });
    }, this.logic = y.getAppState().legalAppManager, this.logic.subscribe(() => this.setState({})), this.state = {
      policyMap: /* @__PURE__ */ new Map(),
      alert: null
    };
  }
  async componentDidMount() {
    this.fetchMarketingPreferences(), this.setPolicyMapFromLogic();
  }
  isPolicyChanged() {
    const { marketingPreferences: i } = this.logic.state;
    return i ? i.filter((e) => {
      if (e.policyId)
        return this.state.policyMap.get(e.policyId) !== e.isAccepted;
    }).length > 0 : !1;
  }
  setPolicyMapFromLogic() {
    const { marketingPreferences: i } = this.logic.state;
    if (i) {
      const e = /* @__PURE__ */ new Map();
      i.map((c) => {
        !c.policyId || !c.isAccepted || e.set(c.policyId, c.isAccepted);
      }), this.setState({ policyMap: e });
    }
  }
  renderPolicySection() {
    const { marketingPreferences: i } = this.logic.state;
    return i !== null && i.length > 0 ? /* @__PURE__ */ t.createElement("section", null, /* @__PURE__ */ t.createElement("h2", null, /* @__PURE__ */ t.createElement(n, { i18nKey: "NotificationPreferencesPage.PolicyTitle" }, "News and Products")), /* @__PURE__ */ t.createElement("ul", { className: "policyList" }, i.map((e) => {
      if (!e || !e.policyId)
        return;
      const c = e.policyId, r = this.state.policyMap.get(c);
      return /* @__PURE__ */ t.createElement("li", { className: "policyItem", key: c }, /* @__PURE__ */ t.createElement(
        p,
        {
          label: e.description,
          isChecked: r,
          isLabelFirst: !0,
          onChange: () => {
            const { policyMap: s } = this.state;
            s.set(c, !r), this.setState({ policyMap: s });
          }
        }
      ));
    }))) : null;
  }
  render() {
    const { isLoading: i, isUpdating: e, marketingPreferences: c } = this.logic.state, { alert: r } = this.state;
    return /* @__PURE__ */ t.createElement(d, { className: "notificationsPage" }, /* @__PURE__ */ t.createElement(f, null, /* @__PURE__ */ t.createElement(n, { i18nKey: "NotificationPreferencesPage.title" }, "Notifications")), /* @__PURE__ */ t.createElement(u, { className: "mainBody" }, !i && !e && !!r && /* @__PURE__ */ t.createElement(o, { type: r.type, className: "alert", onDismiss: () => this.setState({ alert: null }) }, r.element), i && /* @__PURE__ */ t.createElement(h, null), !i && /* @__PURE__ */ t.createElement(
      "form",
      {
        onSubmit: (s) => {
          s.preventDefault(), this.handleSubmit();
        }
      },
      this.renderPolicySection(),
      Array.isArray(c) && c.length > 0 && /* @__PURE__ */ t.createElement("div", { className: "btnContainer" }, /* @__PURE__ */ t.createElement(
        l,
        {
          loadingIndicator: "SPINNER",
          className: "btn-primary btnSave",
          type: "submit",
          disabled: e || !this.isPolicyChanged(),
          isLoading: e
        },
        /* @__PURE__ */ t.createElement(n, { i18nKey: "NotificationPreferencesPage.save" }, "Save")
      )),
      Array.isArray(c) && c.length === 0 && /* @__PURE__ */ t.createElement(n, { i18nKey: "NotificationPreferencesPage.noPreferences" }, "Your notification preferences are unavailable")
    )));
  }
}
export {
  v as default
};
