import { Button as h, ButtonStyle as E, ButtonType as b } from "./accelbyte-widgets107.mjs";
import { Checkbox as y } from "./accelbyte-widgets335.mjs";
import { InputGroup as B } from "./accelbyte-widgets229.mjs";
import { Loading as L } from "./accelbyte-widgets136.mjs";
import { Ctx as Z } from "./accelbyte-widgets33.mjs";
import { AlertType as N } from "./accelbyte-widgets128.mjs";
import e, { useState as l } from "react";
import { PaymentAlert as S } from "./accelbyte-widgets493.mjs";
import "./accelbyte-widgets659.mjs";
const M = ({ onSubmit: c, userProfile: n }) => {
  const [o, d] = l(n && n.zipCode ? n.zipCode : ""), [a, p] = l(!1), [i, f] = l(n && n.zipCode ? !0 : null), [s, r] = l({
    value: null,
    isLoading: !1,
    error: null
  }), u = !i && i !== null, g = async () => {
    if (!s.isLoading) {
      r({ value: null, isLoading: !0, error: null });
      try {
        const t = await Z.getUserManager().updateUserProfile({
          zipCode: o
        });
        r({ value: t || null, isLoading: !1, error: null }), c(o);
      } catch (t) {
        console.error(t), r({ error: t, value: null, isLoading: !1 });
      }
    }
  }, C = () => {
    a ? g() : c(o);
  }, v = (t) => {
    const { value: m } = t.target;
    d(m), f(!!m && !!m.trim());
  }, I = () => {
    p(!a);
  };
  return s.isLoading ? /* @__PURE__ */ e.createElement(L, null) : s.error ? /* @__PURE__ */ e.createElement(
    S,
    {
      type: N.warning,
      message: "Something went wrong, Please retry or contact our administrator if the problem persist",
      onRetry: () => r({ value: null, isLoading: !1, error: null })
    }
  ) : /* @__PURE__ */ e.createElement("div", { className: "billing-info-form" }, /* @__PURE__ */ e.createElement(
    "form",
    {
      onSubmit: (t) => {
        t.preventDefault();
      }
    },
    /* @__PURE__ */ e.createElement("div", { className: `form-group ${u && "error"}` }, /* @__PURE__ */ e.createElement("label", null, "ZIP Code"), /* @__PURE__ */ e.createElement(B, { type: "text", value: o, onChange: v, placeholder: "Input ZIP Code", className: "form-control" }), u && /* @__PURE__ */ e.createElement("div", { className: "error-message" }, "Zip Code is required")),
    /* @__PURE__ */ e.createElement("div", null, /* @__PURE__ */ e.createElement("label", { className: "save-checkbox" }, /* @__PURE__ */ e.createElement(y, { isChecked: a, onChange: I }), "Save billing information for future transaction")),
    /* @__PURE__ */ e.createElement(h, { onClick: C, buttonStyle: E.PRIMARY, buttonType: b.FLAT, disabled: !i }, "CONTINUE")
  ));
};
export {
  M as default
};
