import { h as r, i as o, T as n, a as l, j as p, L as m, f as h } from "./index-162fd1aa.mjs";
import { C as y } from "./Checkbox-5e314906.mjs";
import t from "react";
import { A as d, b as g, a as u } from "./AccountMainSection-4c35f8d7.mjs";
import "./dateFns-2367ee5b.mjs";
import "./validator-61879ab7.mjs";
import "./reactPlayer-2f69f369.mjs";
import "react-router-dom";
import "./reactSlick-cb4a2d39.mjs";
class C 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: r.danger,
          element: /* @__PURE__ */ t.createElement(o, { error: e })
        }
      });
    }, this.notifyAcceptSuccess = () => {
      this.setState({
        alert: {
          type: r.success,
          element: /* @__PURE__ */ t.createElement(n, { i18nKey: "NotificationPreferencesPage.acceptSuccess" }, "Your notification preferences have been successfully updated")
        }
      });
    }, this.handleSubmit = async () => {
      const e = [], { policyMap: s } = this.state, { marketingPreferences: a } = this.logic.state;
      a && a.forEach((c) => {
        !c.policyId || !c.policyVersion || !c.localizedPolicyVersion || e.push({
          policyId: c.policyId,
          policyVersionId: c.policyVersion.id,
          localizedPolicyVersionId: c.localizedPolicyVersion.id,
          isAccepted: !!s.get(c.policyId)
        });
      }), this.logic.updateMarketingPreferences(e).then(() => {
        this.onAcceptSuccess();
      }).catch((c) => {
        this.setErrorAlert(c);
      });
    }, this.logic = l.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((s) => {
        !s.policyId || !s.isAccepted || e.set(s.policyId, s.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 s = e.policyId, a = this.state.policyMap.get(s);
      return /* @__PURE__ */ t.createElement("li", { className: "policyItem", key: s }, /* @__PURE__ */ t.createElement(
        y,
        {
          label: e.description,
          isChecked: a,
          isLabelFirst: !0,
          onChange: () => {
            const { policyMap: c } = this.state;
            c.set(s, !a), this.setState({ policyMap: c });
          }
        }
      ));
    }))) : null;
  }
  render() {
    const { isLoading: i, isUpdating: e, marketingPreferences: s } = this.logic.state, { alert: a } = this.state;
    return /* @__PURE__ */ t.createElement(d, { className: "notificationsPage" }, /* @__PURE__ */ t.createElement(g, null, /* @__PURE__ */ t.createElement(n, { i18nKey: "NotificationPreferencesPage.title" }, "Notifications")), /* @__PURE__ */ t.createElement(u, { className: "mainBody" }, !i && !e && !!a && /* @__PURE__ */ t.createElement(p, { type: a.type, className: "alert", onDismiss: () => this.setState({ alert: null }) }, a.element), i && /* @__PURE__ */ t.createElement(m, null), !i && /* @__PURE__ */ t.createElement(
      "form",
      {
        onSubmit: (c) => {
          c.preventDefault(), this.handleSubmit();
        }
      },
      this.renderPolicySection(),
      Array.isArray(s) && s.length > 0 && /* @__PURE__ */ t.createElement("div", { className: "btnContainer" }, /* @__PURE__ */ t.createElement(
        h,
        {
          loadingIndicator: "SPINNER",
          className: "btn-primary btnSave",
          type: "submit",
          disabled: e || !this.isPolicyChanged(),
          isLoading: e
        },
        /* @__PURE__ */ t.createElement(n, { i18nKey: "NotificationPreferencesPage.save" }, "Save")
      )),
      Array.isArray(s) && s.length === 0 && /* @__PURE__ */ t.createElement(n, { i18nKey: "NotificationPreferencesPage.noPreferences" }, "Your notification preferences are unavailable")
    )));
  }
}
export {
  C as default
};
