import { h as AlertType, i as CommonError, T as Trans, a as Ctx, j as Alert, L as Loading, f as Button } from "./index.js";
import { C as Checkbox } from "./Checkbox.js";
import React__default from "react";
import { A as AccountMainSection, b as AccountMainSectionHeader, a as AccountMainSectionBody } from "./AccountMainSection.js";
import "./dateFns.js";
import "./validator.js";
import "./reactPlayer.js";
import "react-router-dom";
import "./reactSlick.js";
var NotificationPreferencesPage$1 = "";
class NotificationPreferencesPage extends React__default.Component {
  constructor(props) {
    super(props);
    this.fetchMarketingPreferences = async () => {
      try {
        await Promise.all([this.logic.fetchMarketingPreferences(), this.logic.fetchMarketingPreferenceRecord()]);
      } catch (error) {
        this.setErrorAlert(error);
      }
    };
    this.onAcceptSuccess = async () => {
      await this.fetchMarketingPreferences();
      this.setPolicyMapFromLogic();
      this.notifyAcceptSuccess();
    };
    this.setErrorAlert = (error) => {
      this.setState({
        alert: {
          type: AlertType.danger,
          element: /* @__PURE__ */ React__default.createElement(CommonError, {
            error
          })
        }
      });
    };
    this.notifyAcceptSuccess = () => {
      this.setState({
        alert: {
          type: AlertType.success,
          element: /* @__PURE__ */ React__default.createElement(Trans, {
            i18nKey: "NotificationPreferencesPage.acceptSuccess"
          }, "Your notification preferences have been successfully updated")
        }
      });
    };
    this.handleSubmit = async () => {
      const acceptPolicies = [];
      const { policyMap } = this.state;
      const { marketingPreferences: preferences } = this.logic.state;
      if (preferences) {
        preferences.forEach((preference) => {
          if (!preference.policyId || !preference.policyVersion || !preference.localizedPolicyVersion)
            return;
          acceptPolicies.push({
            policyId: preference.policyId,
            policyVersionId: preference.policyVersion.id,
            localizedPolicyVersionId: preference.localizedPolicyVersion.id,
            isAccepted: !!policyMap.get(preference.policyId)
          });
        });
      }
      this.logic.updateMarketingPreferences(acceptPolicies).then(() => {
        this.onAcceptSuccess();
      }).catch((error) => {
        this.setErrorAlert(error);
      });
    };
    this.logic = Ctx.getAppState().legalAppManager;
    this.logic.subscribe(() => this.setState({}));
    this.state = {
      policyMap: /* @__PURE__ */ new Map(),
      alert: null
    };
  }
  async componentDidMount() {
    this.fetchMarketingPreferences();
    this.setPolicyMapFromLogic();
  }
  isPolicyChanged() {
    const { marketingPreferences: preferences } = this.logic.state;
    if (preferences) {
      return preferences.filter((preference) => {
        if (!preference.policyId)
          return;
        return this.state.policyMap.get(preference.policyId) !== preference.isAccepted;
      }).length > 0;
    }
    return false;
  }
  setPolicyMapFromLogic() {
    const { marketingPreferences: preferences } = this.logic.state;
    if (preferences) {
      const policyMap = /* @__PURE__ */ new Map();
      preferences.map((preference) => {
        if (!preference.policyId || !preference.isAccepted)
          return;
        policyMap.set(preference.policyId, preference.isAccepted);
      });
      this.setState({ policyMap });
    }
  }
  renderPolicySection() {
    const { marketingPreferences: preferences } = this.logic.state;
    if (preferences !== null && preferences.length > 0) {
      return /* @__PURE__ */ React__default.createElement("section", null, /* @__PURE__ */ React__default.createElement("h2", null, /* @__PURE__ */ React__default.createElement(Trans, {
        i18nKey: "NotificationPreferencesPage.PolicyTitle"
      }, "News and Products")), /* @__PURE__ */ React__default.createElement("ul", {
        className: "policyList"
      }, preferences.map((preference) => {
        if (!preference || !preference.policyId)
          return;
        const policyId = preference.policyId;
        const checked = this.state.policyMap.get(policyId);
        return /* @__PURE__ */ React__default.createElement("li", {
          className: "policyItem",
          key: policyId
        }, /* @__PURE__ */ React__default.createElement(Checkbox, {
          label: preference.description,
          isChecked: checked,
          isLabelFirst: true,
          onChange: () => {
            const { policyMap } = this.state;
            policyMap.set(policyId, !checked);
            this.setState({ policyMap });
          }
        }));
      })));
    }
    return null;
  }
  render() {
    const { isLoading, isUpdating, marketingPreferences } = this.logic.state;
    const { alert } = this.state;
    return /* @__PURE__ */ React__default.createElement(AccountMainSection, {
      className: "notificationsPage"
    }, /* @__PURE__ */ React__default.createElement(AccountMainSectionHeader, null, /* @__PURE__ */ React__default.createElement(Trans, {
      i18nKey: "NotificationPreferencesPage.title"
    }, "Notifications")), /* @__PURE__ */ React__default.createElement(AccountMainSectionBody, {
      className: "mainBody"
    }, !isLoading && !isUpdating && !!alert && /* @__PURE__ */ React__default.createElement(Alert, {
      type: alert.type,
      className: "alert",
      onDismiss: () => this.setState({ alert: null })
    }, alert.element), isLoading && /* @__PURE__ */ React__default.createElement(Loading, null), !isLoading && /* @__PURE__ */ React__default.createElement("form", {
      onSubmit: (event) => {
        event.preventDefault();
        this.handleSubmit();
      }
    }, this.renderPolicySection(), Array.isArray(marketingPreferences) && marketingPreferences.length > 0 && /* @__PURE__ */ React__default.createElement("div", {
      className: "btnContainer"
    }, /* @__PURE__ */ React__default.createElement(Button, {
      loadingIndicator: "SPINNER",
      className: "btn-primary btnSave",
      type: "submit",
      disabled: isUpdating || !this.isPolicyChanged(),
      isLoading: isUpdating
    }, /* @__PURE__ */ React__default.createElement(Trans, {
      i18nKey: "NotificationPreferencesPage.save"
    }, "Save"))), Array.isArray(marketingPreferences) && marketingPreferences.length === 0 && /* @__PURE__ */ React__default.createElement(Trans, {
      i18nKey: "NotificationPreferencesPage.noPreferences"
    }, "Your notification preferences are unavailable"))));
  }
}
export { NotificationPreferencesPage as default };
