import { DataRetrievalStatus as d } from "./accelbyte-widgets133.mjs";
import { Alert as w } from "./accelbyte-widgets123.mjs";
import { Button as C } from "./accelbyte-widgets107.mjs";
import { CommonError as R } from "./accelbyte-widgets124.mjs";
import { FormGroupError as q, Form as b, FormGroup as v } from "./accelbyte-widgets263.mjs";
import { InputGroup as N } from "./accelbyte-widgets229.mjs";
import { Loading as A } from "./accelbyte-widgets136.mjs";
import { Pagination as L } from "./accelbyte-widgets256.mjs";
import { t as x } from "./accelbyte-widgets58.mjs";
import I from "./accelbyte-widgets210.mjs";
import { OverrideCSS as K } from "./accelbyte-widgets78.mjs";
import { Ctx as c } from "./accelbyte-widgets33.mjs";
import { AlertType as p } from "./accelbyte-widgets128.mjs";
import { AccPath as f } from "./accelbyte-widgets34.mjs";
import { ErrorHelper as G } from "./accelbyte-widgets120.mjs";
import { FeUtils as P } from "./accelbyte-widgets45.mjs";
import F from "classnames";
import { parseISO as g, isPast as T } from "date-fns";
import e from "react";
import { Trans as s } from "react-i18next";
import { AccountMainSection as k, AccountMainSectionHeader as M, AccountMainSectionBody as O } from "./accelbyte-widgets122.mjs";
import { CancelConfirmDialog as U } from "./accelbyte-widgets281.mjs";
import { DownloadConfirmDialog as $ } from "./accelbyte-widgets282.mjs";
import "./accelbyte-widgets283.mjs";
import { requestStatusIcon as H } from "./accelbyte-widgets284.mjs";
import { mockPassword as _, mockUserPersonalData as z } from "./accelbyte-widgets209.mjs";
import { validateComplexPassword as B, ValidateComplexPasswordErrorType as V } from "./accelbyte-widgets285.mjs";
import { Validation as W } from "./accelbyte-widgets269.mjs";
class Y extends e.Component {
  constructor(t) {
    super(t), this.fetchGdprRequestDataList = () => {
      var r;
      const a = (r = c.getAppState().getUser()) == null ? void 0 : r.userId;
      a && this.logic.fetchGdprRequestDataList(a, this.getOffset(this.props.page), this.getLimit()).catch((n) => {
        this.setErrorAlert(n);
      });
    }, this.notifyRequestSuccess = () => {
      this.setState({
        alert: {
          type: p.success,
          element: /* @__PURE__ */ e.createElement(s, { i18nKey: "PersonalDataPage.successRequest" }, "Request sent! Please check the status of your request below.")
        }
      });
    }, this.notifyRequestCancellation = () => {
      this.setState({
        alert: {
          type: p.success,
          element: /* @__PURE__ */ e.createElement(s, { i18nKey: "PersonalDataPage.successCancel" }, "Your personal data request has been canceled")
        }
      });
    }, this.onRequestGdprDataError = (a) => {
      G.extractServiceErrorCode(a) ? this.setErrorAlert(a) : this.setState({
        alert: {
          type: p.danger,
          element: /* @__PURE__ */ e.createElement(s, { i18nKey: "PersonalDataPage.errorRequest" }, "Unable to send the request. Please try again.")
        }
      });
    }, this.setErrorAlert = (a) => {
      this.setState({
        alert: {
          type: p.danger,
          element: /* @__PURE__ */ e.createElement(R, { error: a })
        }
      });
    }, this.clearNotification = () => this.setState({ alert: null }), this.onPasswordChange = (a) => {
      const r = a.target.value;
      this.validation.set("password", B(r)), this.setState({ password: r });
    }, this.renderPagination = () => {
      const { gdprDataRequestList: a } = this.logic.state;
      if (!a)
        return;
      const { Paging: r } = a, n = !!r.Next, i = !!r.Previous, l = this.props.page;
      return /* @__PURE__ */ e.createElement("div", { className: "pagination" }, /* @__PURE__ */ e.createElement(
        L,
        {
          next: n && `${f.PersonalData.link}?page=${l + 1}`,
          prev: i && `${f.PersonalData.link}?page=${l - 1}`
        }
      ));
    }, this.onSubmit = (a) => {
      var n;
      a.preventDefault(), this.setState({ password: "" });
      const r = (n = c.getAppState().getUser()) == null ? void 0 : n.userId;
      r && this.logic.requestGdprData(this.state.password, r).then(() => {
        this.notifyRequestSuccess(), this.fetchGdprRequestDataList();
      }).catch((i) => {
        this.setErrorAlert(i);
      });
    }, this.cancelRequest = (a) => {
      var n;
      const r = (n = c.getAppState().getUser()) == null ? void 0 : n.userId;
      a && r && this.logic.cancelRequestGdprData(a, r).then(() => {
        this.notifyRequestCancellation();
      }).catch((i) => {
        this.setErrorAlert(i);
      }).finally(() => {
        this.fetchGdprRequestDataList(), this.setState({ openCancelDialog: !1 });
      });
    }, this.downloadGdpr = (a, r) => {
      var i;
      const n = (i = c.getAppState().getUser()) == null ? void 0 : i.userId;
      !a || !r || !n || this.logic.downloadGdprData(a, r, n).then((l) => {
        l && window.open(l.URL, "_blank");
      }).catch((l) => {
        this.setErrorAlert(l);
      }).finally(() => {
        this.setState({ openDownloadDialog: !1 });
      });
    };
    const o = c.getAppState().gdprAppManager;
    o.subscribe(() => this.setState({})), this.logic = o, this.state = {
      password: "",
      selectedRequestDate: null,
      openCancelDialog: !1,
      openDownloadDialog: !1,
      alert: null
    }, this.validation = new W(), this.validation.listen(() => this.setState({}));
  }
  componentDidMount() {
    this.props.isPreviewMode || this.fetchGdprRequestDataList();
  }
  formatSelectedDate(t) {
    return `${new Date(t).toISOString().substring(0, 19)}Z`;
  }
  getLimit() {
    return 10;
  }
  getOffset(t) {
    return t ? (t - 1) * this.getLimit() : 0;
  }
  renderDownloadAction(t, o, a) {
    const r = this.props.isPreviewMode;
    return /* @__PURE__ */ e.createElement(e.Fragment, null, t && !o && /* @__PURE__ */ e.createElement(
      "div",
      {
        className: "btnDownload",
        onClick: r ? void 0 : () => this.setState({
          openDownloadDialog: !0,
          selectedRequestDate: this.formatSelectedDate(a)
        })
      },
      /* @__PURE__ */ e.createElement(s, { i18nKey: "PersonalDataPage.action.download" }, "Download")
    ));
  }
  renderCancelAction(t, o, a, r) {
    const n = this.props.isPreviewMode;
    return /* @__PURE__ */ e.createElement(e.Fragment, null, !t && !o && !a ? /* @__PURE__ */ e.createElement(
      "div",
      {
        className: "btnCancel",
        onClick: n ? void 0 : () => this.setState({
          openCancelDialog: !0,
          selectedRequestDate: this.formatSelectedDate(r)
        })
      },
      /* @__PURE__ */ e.createElement(s, { i18nKey: "PersonalDataPage.action.cancel" }, "Cancel")
    ) : /* @__PURE__ */ e.createElement("span", { className: "btnCancel disabled" }, /* @__PURE__ */ e.createElement(s, { i18nKey: "PersonalDataPage.action.cancel" }, "Cancel")));
  }
  renderExpirationDate(t, o, a) {
    return a ? t ? /* @__PURE__ */ e.createElement(s, { i18nKey: "PersonalDataPage.fileExpired" }, "file expired") : P.formatDate(g(o), { type: "dynamic", format: "LLLL" }) : "-";
  }
  renderListDataRequest(t) {
    const o = t.Status === d.enum.Completed, a = t.Status === d.enum.Failed, r = t.Status === d.enum.Canceled, n = T(g(t.DataExpirationDate)), i = d.safeParse(t.Status);
    return /* @__PURE__ */ e.createElement("tr", { key: t.RequestDate, className: "tableRow" }, /* @__PURE__ */ e.createElement("td", { className: "date" }, P.formatDate(g(t.RequestDate), {
      type: "dynamic",
      format: "LL"
    })), /* @__PURE__ */ e.createElement("td", { className: "statusContainer" }, /* @__PURE__ */ e.createElement("span", { className: "statusIcon" }, i.success && H(i.data)), t.Status), /* @__PURE__ */ e.createElement("td", null, this.renderExpirationDate(n, t.DataExpirationDate, o)), /* @__PURE__ */ e.createElement("td", null, this.renderDownloadAction(o, n, t.RequestDate)), /* @__PURE__ */ e.createElement("td", null, this.renderCancelAction(o, a, r, t.RequestDate)));
  }
  renderErrorType(t) {
    return t === V.enum.empty ? /* @__PURE__ */ e.createElement(q, null, /* @__PURE__ */ e.createElement(s, { i18nKey: "PersonalDataPage.inputPasswordForm.inputPasswordError.empty" }, "Input your password to proceed")) : null;
  }
  render() {
    const { gdprDataRequestList: t, isRequestingGdpr: o, isFetchingGdpr: a } = this.logic.state, { password: r, openCancelDialog: n, openDownloadDialog: i, selectedRequestDate: l, alert: u } = this.state, h = this.props.isPreviewMode, m = h ? z : t, E = m ? m.Data.length : 0, y = !this.validation.isAllValid() || !r || o;
    return /* @__PURE__ */ e.createElement(e.Fragment, null, /* @__PURE__ */ e.createElement(k, null, /* @__PURE__ */ e.createElement(M, null, /* @__PURE__ */ e.createElement(s, { i18nKey: "PersonalDataPage.title" }, "Personal Data Request")), /* @__PURE__ */ e.createElement(O, null, /* @__PURE__ */ e.createElement("div", { className: "personalDataPage" }, /* @__PURE__ */ e.createElement("section", { className: "formSection" }, /* @__PURE__ */ e.createElement("div", { className: F("guideHeading", K.TITLE_FONT) }, /* @__PURE__ */ e.createElement(s, { i18nKey: "PersonalDataPage.guideHeading" }, "Download Personal Data")), /* @__PURE__ */ e.createElement("div", { className: "guideText" }, /* @__PURE__ */ e.createElement(s, { i18nKey: "PersonalDataPage.guide" }, "Collecting your data may take up to 28 days. Once your data has been collected, you will have a limited amount of time to download the data.")), /* @__PURE__ */ e.createElement("h3", { className: "formTextTitle" }, /* @__PURE__ */ e.createElement(s, { i18nKey: "PersonalDataPage.inputPasswordTitle" }, "Input your password to proceed")), /* @__PURE__ */ e.createElement(b, { onSubmit: this.onSubmit, className: "form", orientation: "horizontal" }, /* @__PURE__ */ e.createElement(v, null, /* @__PURE__ */ e.createElement(
      N,
      {
        className: "inputRedeem",
        type: "password",
        value: h ? _ : r,
        placeholder: x("PersonalDataPage.form.passwordPlaceholder"),
        onChange: this.onPasswordChange,
        readOnly: h
      }
    ), this.renderErrorType(this.validation.get("password"))), /* @__PURE__ */ e.createElement(C, { disabled: y, className: "btn btn-primary buttonSendRequest", type: "submit" }, o ? /* @__PURE__ */ e.createElement(s, { i18nKey: "PersonalDataPage.processing" }, "Processing...") : /* @__PURE__ */ e.createElement(s, { i18nKey: "PersonalDataPage.buttonAction" }, "Send Request"))), !!u && /* @__PURE__ */ e.createElement(w, { type: u.type, className: "alertContainer", onDismiss: this.clearNotification }, u.element)), /* @__PURE__ */ e.createElement("section", { className: "fileSection" }, a && /* @__PURE__ */ e.createElement("div", { className: "loadingContainer" }, /* @__PURE__ */ e.createElement(A, { className: "loading" })), !a && m && /* @__PURE__ */ e.createElement(e.Fragment, null, /* @__PURE__ */ e.createElement("h1", { className: "title" }, /* @__PURE__ */ e.createElement(s, { i18nKey: "PersonalDataPage.filesTitle" }, "Files"), " (", E, ")"), E > 0 ? /* @__PURE__ */ e.createElement(e.Fragment, null, /* @__PURE__ */ e.createElement("table", null, /* @__PURE__ */ e.createElement("thead", null, /* @__PURE__ */ e.createElement("tr", null, /* @__PURE__ */ e.createElement("th", null, /* @__PURE__ */ e.createElement(s, { i18nKey: "PersonalDataPage.table.date" }, "Date")), /* @__PURE__ */ e.createElement("th", null, /* @__PURE__ */ e.createElement(s, { i18nKey: "PersonalDataPage.table.status" }, "Status")), /* @__PURE__ */ e.createElement("th", null, /* @__PURE__ */ e.createElement(s, { i18nKey: "PersonalDataPage.table.fileExpiration" }, "File expiration")), /* @__PURE__ */ e.createElement("th", { colSpan: 2 }, " "), /* @__PURE__ */ e.createElement("th", null))), /* @__PURE__ */ e.createElement("tbody", null, m.Data.map((S) => this.renderListDataRequest(S)))), this.renderPagination()) : /* @__PURE__ */ e.createElement("p", { className: "noData" }, /* @__PURE__ */ e.createElement(s, { i18nKey: "PersonalDataPage.noData" }, "No files available"))))))), n && /* @__PURE__ */ e.createElement(
      U,
      {
        onCancel: () => this.setState({ openCancelDialog: !1, selectedRequestDate: null }),
        onConfirm: () => this.cancelRequest(l)
      }
    ), i && /* @__PURE__ */ e.createElement(
      $,
      {
        dateRequest: l,
        onCancel: () => this.setState({ openDownloadDialog: !1 }),
        onConfirm: this.downloadGdpr
      }
    ));
  }
}
const Ce = I(Y, (D, t) => D.page !== t.page);
export {
  Ce as default
};
