@agrada_digital/pbm 0.0.90 → 0.0.92

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -94,7 +94,7 @@ function Container({
94
94
  "main",
95
95
  {
96
96
  className: classNames({
97
- "border-3 border-[#44c2c0] flex flex-col items-center justify-center min-w-[var(--min-container)] max-w-[var(--max-container)] w-full h-auto rounded-xl p-4 bg-white gap-4 relative": variant === "main",
97
+ "border-3 border-[#44c2c0] flex flex-col items-center justify-center min-w-(--min-container) max-w-(--max-container) w-full h-auto rounded-xl p-4 bg-white gap-4 relative": variant === "main",
98
98
  "w-full h-auto relative": variant === "simple"
99
99
  }),
100
100
  "data-testid": "test_id_container",
@@ -110,7 +110,7 @@ var Container_default = Container;
110
110
  import classNames2 from "classnames";
111
111
  import { jsx as jsx3, jsxs as jsxs2 } from "react/jsx-runtime";
112
112
  function Footer() {
113
- const { targetProduct, state } = usePBMStore();
113
+ const { targetProduct } = usePBMStore();
114
114
  return /* @__PURE__ */ jsx3("footer", { className: "w-full h-auto relative", id: "footer_pbm", children: /* @__PURE__ */ jsxs2("section", { className: classNames2("flex items-center w-full h-auto gap-4", { "justify-center": targetProduct?.industryLogo, "justify-start": !targetProduct?.industryLogo }), children: [
115
115
  /* @__PURE__ */ jsxs2("section", { className: "w-4/5 h-auto", children: [
116
116
  /* @__PURE__ */ jsx3("h3", { className: "text-start font-semibold text-sm", children: "Economize com o benef\xEDcio do laborat\xF3rio." }),
@@ -146,7 +146,7 @@ var validationSchema = z.object({
146
146
  coupon: z.string({ required_error: "Cupom / ID do Cart\xE3o obrigat\xF3rio." }).optional()
147
147
  });
148
148
 
149
- // src/utils/format.ts
149
+ // src/utils/format-security-number.ts
150
150
  var toFormat = (value) => {
151
151
  const cleanedValue = value.replace(/\D/g, "");
152
152
  if (cleanedValue.length <= 11) {
@@ -154,12 +154,15 @@ var toFormat = (value) => {
154
154
  }
155
155
  };
156
156
 
157
- // src/components/Form/index.tsx
158
- import classNames4 from "classnames";
159
- import { zodResolver } from "@hookform/resolvers/zod";
160
- import { useForm } from "react-hook-form";
161
- import { ArrowRight } from "lucide-react";
162
- import { useState } from "react";
157
+ // src/utils/mapping-internal-states.ts
158
+ var mappingInternalStates = (outContextState) => {
159
+ const status = {
160
+ "acceptance": "isInvalid",
161
+ "industry registration": "isRegistered",
162
+ "active": "isActivated"
163
+ };
164
+ return status[outContextState];
165
+ };
163
166
 
164
167
  // src/services/benefits-with-document.ts
165
168
  import Cookies from "js-cookie";
@@ -206,9 +209,14 @@ function Button(props) {
206
209
  var Button_default = Button;
207
210
 
208
211
  // src/components/Form/index.tsx
212
+ import { zodResolver } from "@hookform/resolvers/zod";
213
+ import { useForm } from "react-hook-form";
214
+ import { ArrowRight } from "lucide-react";
215
+ import classNames4 from "classnames";
216
+ import { Activity, useState } from "react";
209
217
  import { Fragment as Fragment2, jsx as jsx5, jsxs as jsxs3 } from "react/jsx-runtime";
210
- function Form({ setLoading }) {
211
- const { setSecurityNumber, setState, securityNumber, targetProduct, setUrlAcceptTerms, setUrlRegisterIndustry, setCardID } = usePBMStore();
218
+ function Form({ startTransition }) {
219
+ const store = usePBMStore();
212
220
  const [showCardIDField, setShowCardIDField] = useState(false);
213
221
  const {
214
222
  handleSubmit,
@@ -221,64 +229,62 @@ function Form({ setLoading }) {
221
229
  resolver: zodResolver(validationSchema),
222
230
  mode: "onSubmit",
223
231
  defaultValues: {
224
- securityNumber: securityNumber || "",
232
+ securityNumber: store.securityNumber || "",
225
233
  coupon: ""
226
234
  }
227
235
  });
228
- const onSubmitDefault = async (values) => {
229
- if (!showCardIDField) {
230
- setValue("coupon", void 0, { shouldValidate: false });
231
- }
232
- if (showCardIDField && values.coupon != void 0) {
233
- setCardID(values.coupon);
234
- }
235
- setLoading(true);
236
+ const checkSecurityNumberBenefits = async (values) => {
236
237
  try {
237
- if (targetProduct === null) {
238
+ if (store.targetProduct === null) {
238
239
  console.error("PBMLOG: Product is not defined!");
239
240
  return;
240
241
  }
241
- if (!targetProduct.productId) {
242
+ if (!store.targetProduct.productId) {
242
243
  console.error("PBMLOG: Product ID is not defined!");
243
244
  return;
244
245
  }
245
- if (!targetProduct.listPrice) {
246
+ if (!store.targetProduct.listPrice) {
246
247
  console.error("PBMLOG: List Price is not defined!");
247
248
  return;
248
249
  }
250
+ const product = {
251
+ productId: store.targetProduct.productId,
252
+ ean: store.targetProduct.ean,
253
+ requestedQuantity: 1,
254
+ listPrice: store.targetProduct.listPrice,
255
+ netPrice: store.targetProduct.netPrice ?? store.targetProduct.listPrice
256
+ };
249
257
  const response = await BenefitsWithDocument({
250
258
  document: values.securityNumber.replace(/\D/g, ""),
251
- products: [{
252
- productId: targetProduct.productId,
253
- ean: targetProduct.ean,
254
- requestedQuantity: 1,
255
- listPrice: targetProduct.listPrice,
256
- netPrice: targetProduct.netPrice ?? targetProduct.listPrice
257
- }]
259
+ products: [product]
258
260
  });
259
261
  if (response.success) {
260
- const status = {
261
- "acceptance": "isInvalid",
262
- "industry registration": "isRegistered",
263
- "active": "isActivated"
264
- };
265
- setSecurityNumber(values.securityNumber);
266
- setState(status[response.data.product[0].statusCustomer]);
267
- if (status[response.data.product[0].statusCustomer] === "isInvalid") {
268
- setUrlAcceptTerms(response.data.product[0].urlAcceptTerm || void 0);
262
+ const status = mappingInternalStates(response.data.product[0].statusCustomer);
263
+ store.setSecurityNumber(values.securityNumber);
264
+ store.setState(status);
265
+ if (status === "isInvalid") {
266
+ store.setUrlAcceptTerms(response.data.product[0].urlAcceptTerm || void 0);
269
267
  return;
270
268
  }
271
- if (status[response.data.product[0].statusCustomer] === "isRegistered") {
272
- setUrlRegisterIndustry(response.data.product[0].informativeLink);
269
+ if (status === "isRegistered") {
270
+ store.setUrlRegisterIndustry(response.data.product[0].informativeLink);
273
271
  return;
274
272
  }
275
273
  }
276
274
  } catch (error) {
277
275
  console.error("PBMLOG: Error validating document -", error);
278
- } finally {
279
- setLoading(false);
280
276
  }
281
- ;
277
+ };
278
+ const onSubmitDefault = async (values) => {
279
+ if (!showCardIDField) {
280
+ setValue("coupon", void 0, { shouldValidate: false });
281
+ }
282
+ if (showCardIDField && values.coupon != void 0) {
283
+ store.setCardID(values.coupon);
284
+ }
285
+ startTransition(async () => {
286
+ await checkSecurityNumberBenefits(values);
287
+ });
282
288
  };
283
289
  return /* @__PURE__ */ jsxs3(Fragment2, { children: [
284
290
  /* @__PURE__ */ jsxs3(
@@ -317,7 +323,7 @@ function Form({ setLoading }) {
317
323
  });
318
324
  }
319
325
  }),
320
- defaultValue: securityNumber || "",
326
+ defaultValue: store.securityNumber || "",
321
327
  id: "input_security_number_pbm"
322
328
  }
323
329
  ),
@@ -325,7 +331,7 @@ function Form({ setLoading }) {
325
331
  ]
326
332
  }
327
333
  ),
328
- showCardIDField && /* @__PURE__ */ jsxs3(
334
+ /* @__PURE__ */ jsx5(Activity, { mode: showCardIDField ? "visible" : "hidden", children: /* @__PURE__ */ jsxs3(
329
335
  "label",
330
336
  {
331
337
  htmlFor: "coupon",
@@ -356,7 +362,7 @@ function Form({ setLoading }) {
356
362
  errors.coupon && /* @__PURE__ */ jsx5("span", { className: "text-red-400 text-xs font-semibold absolute -bottom-3 left-2 text-nowrap", id: "coupon_form_error", children: errors.coupon.message })
357
363
  ]
358
364
  }
359
- ),
365
+ ) }),
360
366
  /* @__PURE__ */ jsx5(
361
367
  "button",
362
368
  {
@@ -396,7 +402,7 @@ function Form({ setLoading }) {
396
402
  Button_default,
397
403
  {
398
404
  className: "bg-transparent p-0 pl-2 w-auto h-auto text-zinc-600 underline cursor-pointer hover:text-zinc-900 hover:bg-transparent flex items-center justify-start gap-1",
399
- onClick: () => setState("isPreview"),
405
+ onClick: () => store.setState("isPreview"),
400
406
  id: "check_benefits_button",
401
407
  children: [
402
408
  /* @__PURE__ */ jsx5("span", { children: "Consultar Benef\xEDcios" }),
@@ -434,9 +440,6 @@ function Loading({ textColor }) {
434
440
  }
435
441
  var Loading_default = Loading;
436
442
 
437
- // src/components/BenefitsTable/index.tsx
438
- import { useEffect as useEffect2, useState as useState3 } from "react";
439
-
440
443
  // src/components/UI/Title/index.tsx
441
444
  import classNames5 from "classnames";
442
445
  import { jsx as jsx7 } from "react/jsx-runtime";
@@ -457,6 +460,35 @@ function Title(props) {
457
460
  }
458
461
  var Title_default = Title;
459
462
 
463
+ // src/services/benefits-without-document.ts
464
+ import Cookies2 from "js-cookie";
465
+ var CheckBenefistWithoutDocument = async ({ products }) => {
466
+ const API_URL = import.meta.env.VITE_API_URL;
467
+ if (!API_URL) {
468
+ throw new Error("API URL is not defined in environment variables");
469
+ }
470
+ const AUTH_TOKEN = Cookies2.get("pbm-token");
471
+ if (!AUTH_TOKEN) {
472
+ throw new Error("Token is not defined in cookies or is expired");
473
+ }
474
+ const response = await fetch(`${API_URL}/products/genericBenefit`, {
475
+ method: "POST",
476
+ headers: {
477
+ Authorization: `Bearer ${AUTH_TOKEN}`,
478
+ "Content-Type": "application/json"
479
+ },
480
+ body: JSON.stringify({ products })
481
+ });
482
+ const dataResponse = await response.json();
483
+ if (!dataResponse.success) {
484
+ throw new Error(dataResponse.message || "Failed to fetch benefits without document");
485
+ }
486
+ return dataResponse;
487
+ };
488
+
489
+ // src/components/BenefitsTable/index.tsx
490
+ import { useEffect as useEffect2, useState as useState2, useTransition } from "react";
491
+
460
492
  // src/libs/zustand/useModal.tsx
461
493
  import { create } from "zustand";
462
494
  var initialModalState = {
@@ -472,7 +504,7 @@ var createModalStore = (set) => ({
472
504
  var useModal = create(createModalStore);
473
505
 
474
506
  // src/components/BenefitsTable/Item.tsx
475
- import { useCallback, useEffect, useState as useState2 } from "react";
507
+ import { useCallback, useEffect } from "react";
476
508
  import classNames6 from "classnames";
477
509
 
478
510
  // src/components/UI/Icons/index.tsx
@@ -528,7 +560,6 @@ import { jsx as jsx9, jsxs as jsxs6 } from "react/jsx-runtime";
528
560
  function Item({ data, onChange, checked }) {
529
561
  const { setAvailableDiscountSelected, state, isAuthenticatedShopper } = usePBMStore();
530
562
  const { setModal } = useModal();
531
- const [openModalAlert, setOpenModalAlert] = useState2(false);
532
563
  const ID_INPUT = "unity_quantity_" + data.authorizedQuantity;
533
564
  const decimalDiscount = data.discountPercentual / 100;
534
565
  const unitDiscountValue = data.grossPrice * decimalDiscount;
@@ -558,12 +589,15 @@ function Item({ data, onChange, checked }) {
558
589
  useEffect(() => {
559
590
  updateStorageData();
560
591
  }, [updateStorageData]);
561
- const CheckUserIsAuthenticated = () => {
562
- if (isAuthenticatedShopper) return;
563
- setModal({
564
- id: "ShopperIsNotAuthenticated",
565
- open: true
566
- });
592
+ const handleChange = () => {
593
+ if (!isAuthenticatedShopper && state === "isActivated") {
594
+ setModal({
595
+ id: "ShopperIsNotAuthenticated",
596
+ open: true
597
+ });
598
+ return;
599
+ }
600
+ onChange();
567
601
  };
568
602
  return /* @__PURE__ */ jsxs6(
569
603
  "label",
@@ -574,7 +608,6 @@ function Item({ data, onChange, checked }) {
574
608
  { "cursor-not-allowed": state === "isPreview", "cursor-pointer": state === "isActivated" }
575
609
  ),
576
610
  id: "label_benefits_" + ID_INPUT,
577
- onClick: CheckUserIsAuthenticated,
578
611
  children: [
579
612
  /* @__PURE__ */ jsx9(
580
613
  "input",
@@ -584,7 +617,7 @@ function Item({ data, onChange, checked }) {
584
617
  id: ID_INPUT,
585
618
  className: "hidden",
586
619
  checked,
587
- onChange,
620
+ onChange: handleChange,
588
621
  disabled: state === "isPreview"
589
622
  }
590
623
  ),
@@ -617,57 +650,31 @@ function Item({ data, onChange, checked }) {
617
650
  }
618
651
  var Item_default = Item;
619
652
 
620
- // src/services/benefits-without-document.ts
621
- import Cookies2 from "js-cookie";
622
- var CheckBenefistWithoutDocument = async ({ products }) => {
623
- const API_URL = import.meta.env.VITE_API_URL;
624
- if (!API_URL) {
625
- throw new Error("API URL is not defined in environment variables");
626
- }
627
- const AUTH_TOKEN = Cookies2.get("pbm-token");
628
- if (!AUTH_TOKEN) {
629
- throw new Error("Token is not defined in cookies or is expired");
630
- }
631
- const response = await fetch(`${API_URL}/products/genericBenefit`, {
632
- method: "POST",
633
- headers: {
634
- Authorization: `Bearer ${AUTH_TOKEN}`,
635
- "Content-Type": "application/json"
636
- },
637
- body: JSON.stringify({ products })
638
- });
639
- const dataResponse = await response.json();
640
- if (!dataResponse.success) {
641
- throw new Error(dataResponse.message || "Failed to fetch benefits without document");
642
- }
643
- return dataResponse;
644
- };
645
-
646
653
  // src/components/BenefitsTable/index.tsx
647
654
  import { Fragment as Fragment3, jsx as jsx10, jsxs as jsxs7 } from "react/jsx-runtime";
648
655
  function BenefitsTable() {
656
+ const [isPending, startTransition] = useTransition();
649
657
  const { securityNumber, setState, state, targetProduct, customLoginUrl, isAuthenticatedShopper } = usePBMStore();
650
- const [selectedDiscout, setSelectedDiscount] = useState3(null);
651
- const [loading, setLoading] = useState3(true);
652
- const [benefitsItems, setBenefitsItems] = useState3();
658
+ const [selectedDiscout, setSelectedDiscount] = useState2(null);
659
+ const [benefitsItems, setBenefitsItems] = useState2();
653
660
  useEffect2(() => {
661
+ if (!targetProduct?.productId) {
662
+ console.error("PBMLOG: Product ID is not defined on targetProduct");
663
+ return;
664
+ }
665
+ if (!targetProduct.ean) {
666
+ console.error("PBMLOG: EAN is not defined on targetProduct");
667
+ return;
668
+ }
669
+ if (!targetProduct.listPrice) {
670
+ console.error("PBMLOG: List Price is not defined on targetProduct");
671
+ return;
672
+ }
673
+ if (!targetProduct.price) {
674
+ console.error("PBMLOG: Price is not defined on targetProduct");
675
+ return;
676
+ }
654
677
  const fetchDicountsWithoutDocument = async () => {
655
- if (!targetProduct?.productId) {
656
- console.error("PBMLOG: Product ID is not defined on targetProduct");
657
- return;
658
- }
659
- if (!targetProduct.ean) {
660
- console.error("PBMLOG: EAN is not defined on targetProduct");
661
- return;
662
- }
663
- if (!targetProduct.listPrice) {
664
- console.error("PBMLOG: List Price is not defined on targetProduct");
665
- return;
666
- }
667
- if (!targetProduct.price) {
668
- console.error("PBMLOG: Price is not defined on targetProduct");
669
- return;
670
- }
671
678
  try {
672
679
  const data = {
673
680
  productId: Number(targetProduct.productId),
@@ -685,8 +692,6 @@ function BenefitsTable() {
685
692
  } catch (error) {
686
693
  setBenefitsItems(void 0);
687
694
  console.error(error);
688
- } finally {
689
- setLoading(false);
690
695
  }
691
696
  };
692
697
  const fetchDiscountWithDocument = async () => {
@@ -694,22 +699,6 @@ function BenefitsTable() {
694
699
  console.error("PBMLOG: Document is not defined");
695
700
  return;
696
701
  }
697
- if (!targetProduct?.productId) {
698
- console.error("PBMLOG: Product ID is not defined on targetProduct");
699
- return;
700
- }
701
- if (!targetProduct.ean) {
702
- console.error("PBMLOG: EAN is not defined on targetProduct");
703
- return;
704
- }
705
- if (!targetProduct.listPrice) {
706
- console.error("PBMLOG: List Price is not defined on targetProduct");
707
- return;
708
- }
709
- if (!targetProduct.price) {
710
- console.error("PBMLOG: Price is not defined on targetProduct");
711
- return;
712
- }
713
702
  try {
714
703
  const data = {
715
704
  productId: Number(targetProduct.productId),
@@ -727,13 +716,13 @@ function BenefitsTable() {
727
716
  } catch (error) {
728
717
  setBenefitsItems(void 0);
729
718
  console.error(error);
730
- } finally {
731
- setLoading(false);
732
719
  }
733
720
  };
734
- state === "isActivated" ? fetchDiscountWithDocument() : fetchDicountsWithoutDocument();
721
+ startTransition(async () => {
722
+ state === "isActivated" ? await fetchDiscountWithDocument() : await fetchDicountsWithoutDocument();
723
+ });
735
724
  }, []);
736
- if (loading) {
725
+ if (isPending) {
737
726
  return /* @__PURE__ */ jsxs7("main", { className: "flex items-center justify-center gap-4", id: "loading_pbm", children: [
738
727
  /* @__PURE__ */ jsx10(
739
728
  "div",
@@ -765,6 +754,7 @@ function BenefitsTable() {
765
754
  {
766
755
  className: "flex flex-col items-center justify-start w-full gap-4.5",
767
756
  id: "form_benefits_table_pbm",
757
+ onSubmit: (e) => e.preventDefault(),
768
758
  children: [
769
759
  !benefitsItems && /* @__PURE__ */ jsx10("p", { className: "text-sm font-semibold text-start text-zinc-900", id: "benefits_empty_pbm", children: "N\xE3o foi poss\xEDvel encontrar benef\xEDcios para esse produto." }),
770
760
  benefitsItems && benefitsItems.map((item, index) => {
@@ -891,22 +881,13 @@ function SecurityNumberInvalid({ textColor }) {
891
881
  /* @__PURE__ */ jsx13("br", {}),
892
882
  "Ao clicar em \u201CAceitar os termos\u201D, voc\xEA ir\xE1 para uma p\xE1gina externa. Aceite os termos e volte para continuar."
893
883
  ] }),
894
- /* @__PURE__ */ jsx13(
895
- Link_default,
896
- {
897
- href: urlAcceptTerms || "",
898
- children: "Aceitar os termos"
899
- }
900
- )
884
+ /* @__PURE__ */ jsx13(Link_default, { href: urlAcceptTerms || "", children: "Aceitar os termos" })
901
885
  ]
902
886
  }
903
887
  );
904
888
  }
905
889
  var SecurityNumberInvalid_default = SecurityNumberInvalid;
906
890
 
907
- // src/PBM.tsx
908
- import { useCallback as useCallback2, useEffect as useEffect3, useState as useState4 } from "react";
909
-
910
891
  // src/components/SecurityNumberRegitered/index.tsx
911
892
  import { jsx as jsx14, jsxs as jsxs9 } from "react/jsx-runtime";
912
893
  function SecurityNumberRegitered({ textColor }) {
@@ -920,19 +901,87 @@ function SecurityNumberRegitered({ textColor }) {
920
901
  children: [
921
902
  /* @__PURE__ */ jsx14(Title_default, { className: "w-full", textColor, children: "Ops, seu CPF ainda n\xE3o est\xE1 habilitado para este produto." }),
922
903
  /* @__PURE__ */ jsx14(Text_default, { className: "w-full", textColor, children: "Para ativar o benef\xEDcio, clique em \u201CAtivar CPF\u201D e conclua a etapa na p\xE1gina externa. Depois, \xE9 s\xF3 voltar para continuar \u2014 vamos aguardar voc\xEA aqui." }),
923
- /* @__PURE__ */ jsx14(
924
- Link_default,
925
- {
926
- href: urlRegisterIndustry || "",
927
- children: "Ativar CPF"
928
- }
929
- )
904
+ /* @__PURE__ */ jsx14(Link_default, { href: urlRegisterIndustry || "", children: "Ativar CPF" })
930
905
  ]
931
906
  }
932
907
  );
933
908
  }
934
909
  var SecurityNumberRegitered_default = SecurityNumberRegitered;
935
910
 
911
+ // src/components/Errors/ErrorToApplyBenefits.tsx
912
+ import { RefreshCw } from "lucide-react";
913
+ import { jsx as jsx15, jsxs as jsxs10 } from "react/jsx-runtime";
914
+ var ErrorToApplyBenefits = () => {
915
+ return /* @__PURE__ */ jsxs10(Container_default, { variant: "main", children: [
916
+ /* @__PURE__ */ jsx15(Title_default, { children: "Erro ao Aplicar o Benef\xEDcio" }),
917
+ /* @__PURE__ */ jsxs10(Text_default, { textAlign: "center", children: [
918
+ "O produto n\xE3o foi encontrado no sistema. ",
919
+ /* @__PURE__ */ jsx15("br", {}),
920
+ " Por favor, tente novamente mais tarde ou contate o suporte."
921
+ ] }),
922
+ /* @__PURE__ */ jsxs10(
923
+ Button_default,
924
+ {
925
+ className: "bg-transparent p-0 pl-2 w-auto h-auto text-zinc-600 underline cursor-pointer hover:text-zinc-900 hover:bg-transparent flex items-center justify-start gap-1",
926
+ onClick: () => window.location.reload(),
927
+ id: "check_benefits_button",
928
+ children: [
929
+ /* @__PURE__ */ jsx15("span", { children: "Tentar novamente" }),
930
+ /* @__PURE__ */ jsx15(RefreshCw, { size: 16 })
931
+ ]
932
+ }
933
+ )
934
+ ] });
935
+ };
936
+ var ErrorToApplyBenefits_default = ErrorToApplyBenefits;
937
+
938
+ // src/components/Modal/index.tsx
939
+ import classNames9 from "classnames";
940
+ import { jsx as jsx16, jsxs as jsxs11 } from "react/jsx-runtime";
941
+ var Modal = ({ ID }) => {
942
+ const { customLoginUrl } = usePBMStore();
943
+ const { modal, setModal } = useModal();
944
+ return /* @__PURE__ */ jsxs11("main", { className: classNames9(
945
+ "fixed inset-0 flex items-center justify-center z-50 flex-col transition-all shadow bg-black/40",
946
+ {
947
+ "opacity-100 pointer-events-auto": modal.id === ID && modal.open,
948
+ "opacity-0 pointer-events-none": modal.id != ID || !modal.open
949
+ }
950
+ ), children: [
951
+ /* @__PURE__ */ jsx16("div", { className: "absolute inset-0", onClick: () => setModal({ id: "", open: false }) }),
952
+ /* @__PURE__ */ jsxs11("section", { className: "z-10 bg-white gap-2 flex-col items-center-safe justify-center-safe p-8 rounded-xl", children: [
953
+ /* @__PURE__ */ jsx16(Title_default, { textColor: "tomato", textSize: "18px", textAlign: "center", className: "mb-2", children: "Opa! Parece que voc\xEA n\xE3o est\xE1 Logado" }),
954
+ /* @__PURE__ */ jsxs11(Text_default, { className: "mb-2", textAlign: "center", children: [
955
+ "Para aproveitar os benef\xEDcios \xE9 necess\xE1rio realizar o ",
956
+ /* @__PURE__ */ jsx16("strong", { children: "Login" }),
957
+ " ou ",
958
+ /* @__PURE__ */ jsx16("strong", { children: "Cadastro" }),
959
+ " no site!"
960
+ ] }),
961
+ /* @__PURE__ */ jsxs11("section", { className: "flex items-center-safe justify-center-safe gap-4", children: [
962
+ /* @__PURE__ */ jsx16(Button_default, { className: "bg-gray-600 hover:bg-gray-500", onClick: () => setModal({ id: "", open: false }), children: "Seguir sem Benef\xEDcios" }),
963
+ /* @__PURE__ */ jsx16(Link_default, { href: customLoginUrl || "/login", children: "Aproveitar Benef\xEDcios" })
964
+ ] })
965
+ ] })
966
+ ] });
967
+ };
968
+ var Modal_default = Modal;
969
+
970
+ // src/PBM.tsx
971
+ import Skeleton from "@mui/material/Skeleton";
972
+
973
+ // src/utils/format-price.ts
974
+ var formatedPrice = (price) => {
975
+ const result = Number(String(price).replace(",", "."));
976
+ return result;
977
+ };
978
+
979
+ // src/PBM.tsx
980
+ import { useTransition as useTransition2 } from "react";
981
+
982
+ // src/hooks/useAppStartup.tsx
983
+ import { useCallback as useCallback2, useEffect as useEffect3, useState as useState3 } from "react";
984
+
936
985
  // src/services/authorization.ts
937
986
  import Cookies3 from "js-cookie";
938
987
  var GetAuthorization = async ({ clientID }) => {
@@ -964,9 +1013,6 @@ var GetAuthorization = async ({ clientID }) => {
964
1013
  return data;
965
1014
  };
966
1015
 
967
- // src/PBM.tsx
968
- import { RefreshCw } from "lucide-react";
969
-
970
1016
  // src/services/get-product-by-ean.ts
971
1017
  import Cookies4 from "js-cookie";
972
1018
  var GetProductByEAN = async ({ PRODUCT_EAN }) => {
@@ -989,41 +1035,6 @@ var GetProductByEAN = async ({ PRODUCT_EAN }) => {
989
1035
  return dataResponse;
990
1036
  };
991
1037
 
992
- // src/PBM.tsx
993
- import Skeleton from "@mui/material/Skeleton";
994
-
995
- // src/components/Modal/index.tsx
996
- import classNames9 from "classnames";
997
- import { jsx as jsx15, jsxs as jsxs10 } from "react/jsx-runtime";
998
- var Modal = ({ ID }) => {
999
- const { customLoginUrl } = usePBMStore();
1000
- const { modal, setModal } = useModal();
1001
- return /* @__PURE__ */ jsxs10("main", { className: classNames9(
1002
- "fixed inset-0 flex items-center justify-center z-50 flex-col transition-all shadow bg-black/40",
1003
- {
1004
- "opacity-100 pointer-events-auto": modal.id === ID && modal.open,
1005
- "opacity-0 pointer-events-none": modal.id != ID || !modal.open
1006
- }
1007
- ), children: [
1008
- /* @__PURE__ */ jsx15("div", { className: "absolute inset-0", onClick: () => setModal({ id: "", open: false }) }),
1009
- /* @__PURE__ */ jsxs10("section", { className: "z-10 bg-white gap-2 flex-col items-center-safe justify-center-safe p-8 rounded-xl", children: [
1010
- /* @__PURE__ */ jsx15(Title_default, { textColor: "tomato", textSize: "18px", textAlign: "center", className: "mb-2", children: "Opa! Parece que voc\xEA n\xE3o est\xE1 Logado" }),
1011
- /* @__PURE__ */ jsxs10(Text_default, { className: "mb-2", textAlign: "center", children: [
1012
- "Para aproveitar os benef\xEDcios \xE9 necess\xE1rio realizar o ",
1013
- /* @__PURE__ */ jsx15("strong", { children: "Login" }),
1014
- " ou ",
1015
- /* @__PURE__ */ jsx15("strong", { children: "Cadastro" }),
1016
- " no site!"
1017
- ] }),
1018
- /* @__PURE__ */ jsxs10("section", { className: "flex items-center-safe justify-center-safe gap-4", children: [
1019
- /* @__PURE__ */ jsx15(Button_default, { className: "bg-gray-600 hover:bg-gray-500", onClick: () => setModal({ id: "", open: false }), children: "Seguir sem Benef\xEDcios" }),
1020
- /* @__PURE__ */ jsx15(Link_default, { href: customLoginUrl || "/login", children: "Aproveitar Benef\xEDcios" })
1021
- ] })
1022
- ] })
1023
- ] });
1024
- };
1025
- var Modal_default = Modal;
1026
-
1027
1038
  // src/services/get-list-products.ts
1028
1039
  import Cookies5 from "js-cookie";
1029
1040
  var GetProductsWithBenefits = async () => {
@@ -1046,44 +1057,33 @@ var GetProductsWithBenefits = async () => {
1046
1057
  return dataResponse;
1047
1058
  };
1048
1059
 
1049
- // src/PBM.tsx
1050
- import { jsx as jsx16, jsxs as jsxs11 } from "react/jsx-runtime";
1051
- function PBM({
1052
- originalProductPrice,
1053
- clientID,
1054
- eanProduct,
1055
- isAuthenticatedShopper,
1056
- customLoginUrl
1057
- }) {
1058
- const formatedOriginalProductPrice = Number(
1059
- String(originalProductPrice).replace(",", ".")
1060
- );
1061
- const [isReady, setIsReady] = useState4(false);
1062
- const [loading, setLoading] = useState4(false);
1063
- const { state, setTargetProduct, targetProduct, setIsAuthenticatedShopper, setCustomLoginUrl } = usePBMStore();
1064
- const [EanProductExist, setEanProductExist] = useState4(true);
1065
- const [IsValid, setIsValid] = useState4(false);
1060
+ // src/hooks/useAppStartup.tsx
1061
+ var useAppStartup = (props) => {
1062
+ const { setTargetProduct, targetProduct, setIsAuthenticatedShopper, setCustomLoginUrl } = usePBMStore();
1063
+ const [IsReady, setIsReady] = useState3(false);
1064
+ const [IsValid, setIsValid] = useState3(false);
1065
+ const [EanProductExist, setEanProductExist] = useState3(true);
1066
1066
  const fetchValidateProduct = useCallback2(async () => {
1067
1067
  try {
1068
1068
  const response = await GetProductsWithBenefits();
1069
1069
  if (response.success) {
1070
1070
  const data = response.data;
1071
- const hasProductInList = data.some((product) => product.ean == eanProduct);
1071
+ const hasProductInList = data.some((product) => product.ean == props.eanProduct);
1072
1072
  setIsValid(hasProductInList);
1073
1073
  }
1074
1074
  } catch (error) {
1075
1075
  console.error(error);
1076
1076
  }
1077
- }, [eanProduct, IsValid]);
1077
+ }, [props.eanProduct, IsValid]);
1078
1078
  const fetchProductByEan = async () => {
1079
1079
  try {
1080
1080
  if (!IsValid) return;
1081
- if (!eanProduct) {
1081
+ if (!props.eanProduct) {
1082
1082
  console.error("PBMLOG: Ean is not defined.");
1083
1083
  setEanProductExist(false);
1084
1084
  return;
1085
1085
  }
1086
- const response = await GetProductByEAN({ PRODUCT_EAN: eanProduct });
1086
+ const response = await GetProductByEAN({ PRODUCT_EAN: props.eanProduct });
1087
1087
  if (response.success && response.data) {
1088
1088
  setEanProductExist(true);
1089
1089
  const { pbm, sku, ...targetProductNewData } = response.data;
@@ -1094,10 +1094,10 @@ function PBM({
1094
1094
  informativeMessage: pbm.informativeMessage ?? "",
1095
1095
  discountMax: pbm.discountMax ?? 0,
1096
1096
  industryLogo: pbm.imageLink ?? void 0,
1097
- ean: eanProduct
1097
+ ean: props.eanProduct
1098
1098
  });
1099
- setIsAuthenticatedShopper(isAuthenticatedShopper);
1100
- setCustomLoginUrl(customLoginUrl);
1099
+ setIsAuthenticatedShopper(props.isAuthenticatedShopper);
1100
+ setCustomLoginUrl(props.customLoginUrl);
1101
1101
  }
1102
1102
  } catch (error) {
1103
1103
  console.error(error);
@@ -1105,9 +1105,9 @@ function PBM({
1105
1105
  setIsReady(true);
1106
1106
  }
1107
1107
  };
1108
- const handleAuthorizationRequest = useCallback2(async () => {
1108
+ const fetchAuthorizationRequest = useCallback2(async () => {
1109
1109
  try {
1110
- const response = await GetAuthorization({ clientID });
1110
+ const response = await GetAuthorization({ clientID: props.clientID });
1111
1111
  if (response.success) {
1112
1112
  fetchValidateProduct();
1113
1113
  fetchProductByEan();
@@ -1117,56 +1117,40 @@ function PBM({
1117
1117
  } catch (error) {
1118
1118
  console.error("Error fetching authorization:", error);
1119
1119
  }
1120
- }, [clientID, IsValid]);
1120
+ }, [props.clientID, IsValid]);
1121
1121
  useEffect3(() => {
1122
- handleAuthorizationRequest();
1123
- }, [handleAuthorizationRequest]);
1124
- if (!isReady) {
1125
- return /* @__PURE__ */ jsx16(Skeleton, { variant: "rectangular", style: { borderRadius: "12px" }, width: 460, height: 358 });
1122
+ fetchAuthorizationRequest();
1123
+ }, [fetchAuthorizationRequest]);
1124
+ return { IsReady, IsValid, EanProductExist };
1125
+ };
1126
+ var useAppStartup_default = useAppStartup;
1127
+
1128
+ // src/PBM.tsx
1129
+ import { Fragment as Fragment4, jsx as jsx17, jsxs as jsxs12 } from "react/jsx-runtime";
1130
+ function PBM(props) {
1131
+ const { state } = usePBMStore();
1132
+ const { IsReady, EanProductExist, IsValid } = useAppStartup_default(props);
1133
+ const [isPending, startTransition] = useTransition2();
1134
+ const formatedProductPrice = formatedPrice(props.originalProductPrice);
1135
+ if (!IsReady) {
1136
+ return /* @__PURE__ */ jsx17(Skeleton, { variant: "rectangular", style: { borderRadius: "12px" }, width: 460, height: 358 });
1126
1137
  }
1127
1138
  if (!EanProductExist) {
1128
- return /* @__PURE__ */ jsx16("div", { id: "pbm-library-root", children: /* @__PURE__ */ jsxs11(Container_default, { variant: "main", children: [
1129
- /* @__PURE__ */ jsx16(Title_default, { children: "Erro ao Aplicar o Benef\xEDcio" }),
1130
- /* @__PURE__ */ jsxs11(Text_default, { textAlign: "center", children: [
1131
- "O produto n\xE3o foi encontrado no sistema. ",
1132
- /* @__PURE__ */ jsx16("br", {}),
1133
- " Por favor, tente novamente mais tarde ou contate o suporte."
1134
- ] }),
1135
- /* @__PURE__ */ jsxs11(
1136
- Button_default,
1137
- {
1138
- className: "bg-transparent p-0 pl-2 w-auto h-auto text-zinc-600 underline cursor-pointer hover:text-zinc-900 hover:bg-transparent flex items-center justify-start gap-1",
1139
- onClick: () => window.location.reload(),
1140
- id: "check_benefits_button",
1141
- children: [
1142
- /* @__PURE__ */ jsx16("span", { children: "Tentar novamente" }),
1143
- /* @__PURE__ */ jsx16(RefreshCw, { size: 16 })
1144
- ]
1145
- }
1146
- )
1147
- ] }) });
1139
+ return /* @__PURE__ */ jsx17("div", { id: "pbm-library-root", children: /* @__PURE__ */ jsx17(ErrorToApplyBenefits_default, {}) });
1148
1140
  }
1149
- if (!IsValid) {
1150
- return;
1151
- }
1152
- const RenderStateComponent = () => {
1153
- const StateMap = {
1154
- "isEmpty": /* @__PURE__ */ jsx16(Form_default, { setLoading }),
1155
- "isInvalid": /* @__PURE__ */ jsx16(SecurityNumberInvalid_default, {}),
1156
- "isRegistered": /* @__PURE__ */ jsx16(SecurityNumberRegitered_default, {}),
1157
- "isActivated": /* @__PURE__ */ jsx16(BenefitsTable_default, {}),
1158
- "isPreview": /* @__PURE__ */ jsx16(BenefitsTable_default, {})
1159
- };
1160
- if (!loading) return StateMap[state];
1161
- else return /* @__PURE__ */ jsx16(Loading_default, {});
1162
- };
1163
- return /* @__PURE__ */ jsxs11("div", { id: "pbm-library-root", children: [
1164
- /* @__PURE__ */ jsxs11(Container_default, { variant: "main", children: [
1165
- /* @__PURE__ */ jsx16(Header_default, { originalProductPrice: formatedOriginalProductPrice || 0 }),
1166
- /* @__PURE__ */ jsx16(Container_default, { variant: "simple", children: /* @__PURE__ */ jsx16(RenderStateComponent, {}) }),
1167
- /* @__PURE__ */ jsx16(Footer_default, {})
1141
+ if (!IsValid) return;
1142
+ return /* @__PURE__ */ jsxs12("div", { id: "pbm-library-root", children: [
1143
+ /* @__PURE__ */ jsxs12(Container_default, { variant: "main", children: [
1144
+ /* @__PURE__ */ jsx17(Header_default, { originalProductPrice: formatedProductPrice || 0 }),
1145
+ /* @__PURE__ */ jsx17(Container_default, { variant: "simple", children: isPending ? /* @__PURE__ */ jsx17(Loading_default, {}) : /* @__PURE__ */ jsxs12(Fragment4, { children: [
1146
+ state === "isEmpty" && /* @__PURE__ */ jsx17(Form_default, { startTransition }),
1147
+ state === "isInvalid" && /* @__PURE__ */ jsx17(SecurityNumberInvalid_default, {}),
1148
+ state === "isRegistered" && /* @__PURE__ */ jsx17(SecurityNumberRegitered_default, {}),
1149
+ (state === "isActivated" || state === "isPreview") && /* @__PURE__ */ jsx17(BenefitsTable_default, {})
1150
+ ] }) }),
1151
+ /* @__PURE__ */ jsx17(Footer_default, {})
1168
1152
  ] }),
1169
- /* @__PURE__ */ jsx16(Modal_default, { ID: "ShopperIsNotAuthenticated" })
1153
+ /* @__PURE__ */ jsx17(Modal_default, { ID: "ShopperIsNotAuthenticated" })
1170
1154
  ] });
1171
1155
  }
1172
1156
  var PBM_default = PBM;