import { c as P, p as j, h as k, f as L, i as B, k as G, b as h, d as N, u as D } from "./AssetItem-ufNA-vj9.js";
import { a as K } from "./index-client-DOkVNeOk.js";
import { t as I, n as z, D as C, m as x, v as F, o as H, p as Q, z as R, w as b, E as T, x as O, y as W, r as M, l as X, I as w, K as U, L as d, N as i, O as u, bT as Z, bU as q, bu as ee, bB as te, P as se, F as ae, W as re, bM as ne, bq as y, bC as oe } from "./index-DRgrWhNH.js";
import { w as Y } from "./ProgressVideo.service-BmjtnZZl.js";
import { c as ce, s as ie, S as le, g as de } from "./index-D5XO39uY.js";
import { C as ue } from "./ContentHeader-B8i-EqEU.js";
import { L as pe } from "./Loader-Cz9tCQ0T.js";
import { N as fe } from "./constants-ByF0HyKB.js";
var _e = L('<div class="crtw-w-full crtw-h-64 crtw-rounded-[10px] crtw-flex crtw-items-center crtw-justify-center" style="background-color: rgb(238, 238, 238)"><!></div>'), Se = L('<!> <div class="crtw-w-full"><!> <div id="cpuiScanner" style="background-color: rgb(238, 238, 238)"></div></div>', 1);
function J(e, t) {
  if (new.target) return P({ component: J, ...e });
  I(t, !1);
  const s = x();
  let o = j(t, "options", 12);
  z(() => F(o()), () => {
    C(s, o().isScannerLoaded);
  }), H();
  var f = {
    get options() {
      return o();
    },
    set options(r) {
      o(r), X();
    },
    $set: D,
    $on: (r, c) => N(t, r, c)
  };
  k();
  var v = Se(), m = Q(v);
  ue(m, { text: "Scan PassScan Code" });
  var g = R(m, 2), l = b(g);
  {
    var a = (r) => {
      var c = _e(), S = b(c);
      pe(S, {}), O(c), h(r, c);
    };
    B(l, (r) => {
      T(s) || r(a);
    });
  }
  var p = R(l, 2);
  let n;
  return O(g), W(() => n = G(p, 1, "crtw-w-full crtw-h-auto crtw-rounded-lg crtw-flex crtw-items-center crtw-justify-center", null, n, { "crtw-hidden": !T(s) })), h(e, v), M(f);
}
const ye = ({ userAddress: e, amount: t }) => {
  d.set({ to: e, amount: t }), i.set(u.SEND_TOKENS);
}, we = async (e) => {
  const { requestAirDropCoin: t } = w(q);
  try {
    d.set({ loaderText: "Issuing AirDropped Token..." }), i.set(u.LOADER), await t(e), await Y(), d.set({ subtitle: "The air drop was successful!", receipt: !1 }), i.set(u.PAYMENT_RESULT);
  } catch (s) {
    d.set({ errorMessage: s.message }), i.set(u.ERROR);
  }
}, ve = async (e) => {
  const t = await Z(e.packageId, e.assetId), { requestAirDropAsset: s } = w(q);
  try {
    d.set({ loaderText: `AirDropping Token: ${t?.name || e.title || ""}` }), i.set(u.LOADER), await s(e), await Y(), d.set({
      subtitle: `Your account has a new token: ${t?.name || e.title || ""}`,
      tokenImage: t?.assetImage,
      receipt: !1
    }), i.set(u.PAYMENT_RESULT);
  } catch (o) {
    console.log(o), d.set({ errorMessage: o.message }), i.set(u.ERROR);
  }
}, me = ({
  title: e,
  assets: t,
  isCardDisabled: s
}) => {
  d.set({
    ...s ? {
      payments: {
        card: {
          disabled: !0
        }
      }
    } : {},
    subtitle: e,
    assets: t
  }), i.set(u.PAYMENT);
}, ge = async ({ eventId: e, packageId: t }) => {
  const { requestLoyaltyPoints: s } = w(U);
  try {
    d.set({ loaderText: "Issuing Loyalty points..", progressVideos: [] }), i.set(u.LOADER), s && await s(e, t), d.set({ subtitle: "The request for loyalty points was successful!", description: "", receipt: !1 }), i.set(u.PAYMENT_RESULT);
  } catch (o) {
    d.set({ description: o.message, title: "The request failed" }), i.set(u.ERROR);
  }
}, Te = async (e, t) => {
  const s = await fetch(
    `https://api.staging.credenza3.com/keyval/key/${e}?client_id=${t}`,
    {
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${w(ee)}`
      },
      method: "GET"
    }
  );
  if (!s.ok) throw new Error("Failed to get key value");
  return await s.json();
}, Ae = async (e, t) => {
  const o = (await Te(e, t)).items[0].val;
  return [JSON.parse(o).scanType, o];
};
function Ee(e, t) {
  if (new.target) return P({ component: Ee, ...e });
  I(t, !1);
  var s = this && this.__awaiter || function(l, a, p, n) {
    function r(c) {
      return c instanceof p ? c : new p(function(S) {
        S(c);
      });
    }
    return new (p || (p = Promise))(function(c, S) {
      function $(_) {
        try {
          A(n.next(_));
        } catch (E) {
          S(E);
        }
      }
      function V(_) {
        try {
          A(n.throw(_));
        } catch (E) {
          S(E);
        }
      }
      function A(_) {
        _.done ? c(_.value) : r(_.value).then($, V);
      }
      A((n = n.apply(l, a || [])).next());
    });
  };
  const { close: o } = w(U);
  let f, v = x(!1);
  const m = (l) => s(void 0, [l], void 0, function* ({ message: a, sub: p, scanType: n }) {
    if (!n)
      try {
        const r = w(re);
        [n, a] = yield Ae(a, r.clientId);
      } catch {
        return ne(fe(a), "failure"), f.close();
      }
    switch (f.close(), n) {
      case y.PASSPORT_ID_PASS:
      case y.PASSPORT_ID: {
        const r = JSON.parse(a), { address: c } = yield oe(p);
        ye({
          userAddress: c,
          amount: r?.amount
        });
        break;
      }
      case y.TOKENS_PURCHASE: {
        me(JSON.parse(a));
        break;
      }
      case y.AIR_DROP_COIN: {
        yield we(JSON.parse(a));
        break;
      }
      case y.AIR_DROP_ASSET: {
        yield ve(JSON.parse(a));
        break;
      }
      case y.REQUEST_LOYALTY_POINTS: {
        yield ge(JSON.parse(a));
        break;
      }
      default:
        o(), i.set(null);
    }
  });
  K(() => s(void 0, void 0, void 0, function* () {
    const { scanner: l, events: a } = yield ce(le);
    f = l, te(se.UI_CLOSED, () => {
      f.close(), i.set(null);
    }), yield f.scan({ isCloseButton: !1 }), C(v, !0), ie(f, a, {
      onCapture: m,
      onError: (p) => {
        const n = de(p);
        n?.includes("Permission") && alert("In order to scan please allow the current page to access your camera and try again.");
      }
    });
  }));
  var g = {
    $set: D,
    $on: (l, a) => N(t, l, a)
  };
  k();
  {
    let l = ae(() => ({ isScannerLoaded: T(v) }));
    J(e, {
      get options() {
        return T(l);
      }
    });
  }
  return M(g);
}
export {
  Ee as default
};
//# sourceMappingURL=Scanner-nHSA3Q2d.js.map
