@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.js CHANGED
@@ -131,7 +131,7 @@ function Container({
131
131
  "main",
132
132
  {
133
133
  className: (0, import_classnames.default)({
134
- "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",
134
+ "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",
135
135
  "w-full h-auto relative": variant === "simple"
136
136
  }),
137
137
  "data-testid": "test_id_container",
@@ -147,7 +147,7 @@ var Container_default = Container;
147
147
  var import_classnames2 = __toESM(require("classnames"));
148
148
  var import_jsx_runtime3 = require("react/jsx-runtime");
149
149
  function Footer() {
150
- const { targetProduct, state } = usePBMStore();
150
+ const { targetProduct } = usePBMStore();
151
151
  return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("footer", { className: "w-full h-auto relative", id: "footer_pbm", children: /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("section", { className: (0, import_classnames2.default)("flex items-center w-full h-auto gap-4", { "justify-center": targetProduct?.industryLogo, "justify-start": !targetProduct?.industryLogo }), children: [
152
152
  /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("section", { className: "w-4/5 h-auto", children: [
153
153
  /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("h3", { className: "text-start font-semibold text-sm", children: "Economize com o benef\xEDcio do laborat\xF3rio." }),
@@ -183,7 +183,7 @@ var validationSchema = import_zod.z.object({
183
183
  coupon: import_zod.z.string({ required_error: "Cupom / ID do Cart\xE3o obrigat\xF3rio." }).optional()
184
184
  });
185
185
 
186
- // src/utils/format.ts
186
+ // src/utils/format-security-number.ts
187
187
  var toFormat = (value) => {
188
188
  const cleanedValue = value.replace(/\D/g, "");
189
189
  if (cleanedValue.length <= 11) {
@@ -191,12 +191,15 @@ var toFormat = (value) => {
191
191
  }
192
192
  };
193
193
 
194
- // src/components/Form/index.tsx
195
- var import_classnames4 = __toESM(require("classnames"));
196
- var import_zod2 = require("@hookform/resolvers/zod");
197
- var import_react_hook_form = require("react-hook-form");
198
- var import_lucide_react = require("lucide-react");
199
- var import_react2 = require("react");
194
+ // src/utils/mapping-internal-states.ts
195
+ var mappingInternalStates = (outContextState) => {
196
+ const status = {
197
+ "acceptance": "isInvalid",
198
+ "industry registration": "isRegistered",
199
+ "active": "isActivated"
200
+ };
201
+ return status[outContextState];
202
+ };
200
203
 
201
204
  // src/services/benefits-with-document.ts
202
205
  var import_js_cookie = __toESM(require("js-cookie"));
@@ -244,9 +247,14 @@ function Button(props) {
244
247
  var Button_default = Button;
245
248
 
246
249
  // src/components/Form/index.tsx
250
+ var import_zod2 = require("@hookform/resolvers/zod");
251
+ var import_react_hook_form = require("react-hook-form");
252
+ var import_lucide_react = require("lucide-react");
253
+ var import_classnames4 = __toESM(require("classnames"));
254
+ var import_react2 = require("react");
247
255
  var import_jsx_runtime5 = require("react/jsx-runtime");
248
- function Form({ setLoading }) {
249
- const { setSecurityNumber, setState, securityNumber, targetProduct, setUrlAcceptTerms, setUrlRegisterIndustry, setCardID } = usePBMStore();
256
+ function Form({ startTransition }) {
257
+ const store = usePBMStore();
250
258
  const [showCardIDField, setShowCardIDField] = (0, import_react2.useState)(false);
251
259
  const {
252
260
  handleSubmit,
@@ -259,64 +267,62 @@ function Form({ setLoading }) {
259
267
  resolver: (0, import_zod2.zodResolver)(validationSchema),
260
268
  mode: "onSubmit",
261
269
  defaultValues: {
262
- securityNumber: securityNumber || "",
270
+ securityNumber: store.securityNumber || "",
263
271
  coupon: ""
264
272
  }
265
273
  });
266
- const onSubmitDefault = async (values) => {
267
- if (!showCardIDField) {
268
- setValue("coupon", void 0, { shouldValidate: false });
269
- }
270
- if (showCardIDField && values.coupon != void 0) {
271
- setCardID(values.coupon);
272
- }
273
- setLoading(true);
274
+ const checkSecurityNumberBenefits = async (values) => {
274
275
  try {
275
- if (targetProduct === null) {
276
+ if (store.targetProduct === null) {
276
277
  console.error("PBMLOG: Product is not defined!");
277
278
  return;
278
279
  }
279
- if (!targetProduct.productId) {
280
+ if (!store.targetProduct.productId) {
280
281
  console.error("PBMLOG: Product ID is not defined!");
281
282
  return;
282
283
  }
283
- if (!targetProduct.listPrice) {
284
+ if (!store.targetProduct.listPrice) {
284
285
  console.error("PBMLOG: List Price is not defined!");
285
286
  return;
286
287
  }
288
+ const product = {
289
+ productId: store.targetProduct.productId,
290
+ ean: store.targetProduct.ean,
291
+ requestedQuantity: 1,
292
+ listPrice: store.targetProduct.listPrice,
293
+ netPrice: store.targetProduct.netPrice ?? store.targetProduct.listPrice
294
+ };
287
295
  const response = await BenefitsWithDocument({
288
296
  document: values.securityNumber.replace(/\D/g, ""),
289
- products: [{
290
- productId: targetProduct.productId,
291
- ean: targetProduct.ean,
292
- requestedQuantity: 1,
293
- listPrice: targetProduct.listPrice,
294
- netPrice: targetProduct.netPrice ?? targetProduct.listPrice
295
- }]
297
+ products: [product]
296
298
  });
297
299
  if (response.success) {
298
- const status = {
299
- "acceptance": "isInvalid",
300
- "industry registration": "isRegistered",
301
- "active": "isActivated"
302
- };
303
- setSecurityNumber(values.securityNumber);
304
- setState(status[response.data.product[0].statusCustomer]);
305
- if (status[response.data.product[0].statusCustomer] === "isInvalid") {
306
- setUrlAcceptTerms(response.data.product[0].urlAcceptTerm || void 0);
300
+ const status = mappingInternalStates(response.data.product[0].statusCustomer);
301
+ store.setSecurityNumber(values.securityNumber);
302
+ store.setState(status);
303
+ if (status === "isInvalid") {
304
+ store.setUrlAcceptTerms(response.data.product[0].urlAcceptTerm || void 0);
307
305
  return;
308
306
  }
309
- if (status[response.data.product[0].statusCustomer] === "isRegistered") {
310
- setUrlRegisterIndustry(response.data.product[0].informativeLink);
307
+ if (status === "isRegistered") {
308
+ store.setUrlRegisterIndustry(response.data.product[0].informativeLink);
311
309
  return;
312
310
  }
313
311
  }
314
312
  } catch (error) {
315
313
  console.error("PBMLOG: Error validating document -", error);
316
- } finally {
317
- setLoading(false);
318
314
  }
319
- ;
315
+ };
316
+ const onSubmitDefault = async (values) => {
317
+ if (!showCardIDField) {
318
+ setValue("coupon", void 0, { shouldValidate: false });
319
+ }
320
+ if (showCardIDField && values.coupon != void 0) {
321
+ store.setCardID(values.coupon);
322
+ }
323
+ startTransition(async () => {
324
+ await checkSecurityNumberBenefits(values);
325
+ });
320
326
  };
321
327
  return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(import_jsx_runtime5.Fragment, { children: [
322
328
  /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(
@@ -355,7 +361,7 @@ function Form({ setLoading }) {
355
361
  });
356
362
  }
357
363
  }),
358
- defaultValue: securityNumber || "",
364
+ defaultValue: store.securityNumber || "",
359
365
  id: "input_security_number_pbm"
360
366
  }
361
367
  ),
@@ -363,7 +369,7 @@ function Form({ setLoading }) {
363
369
  ]
364
370
  }
365
371
  ),
366
- showCardIDField && /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(
372
+ /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(import_react2.Activity, { mode: showCardIDField ? "visible" : "hidden", children: /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)(
367
373
  "label",
368
374
  {
369
375
  htmlFor: "coupon",
@@ -394,7 +400,7 @@ function Form({ setLoading }) {
394
400
  errors.coupon && /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("span", { className: "text-red-400 text-xs font-semibold absolute -bottom-3 left-2 text-nowrap", id: "coupon_form_error", children: errors.coupon.message })
395
401
  ]
396
402
  }
397
- ),
403
+ ) }),
398
404
  /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
399
405
  "button",
400
406
  {
@@ -434,7 +440,7 @@ function Form({ setLoading }) {
434
440
  Button_default,
435
441
  {
436
442
  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",
437
- onClick: () => setState("isPreview"),
443
+ onClick: () => store.setState("isPreview"),
438
444
  id: "check_benefits_button",
439
445
  children: [
440
446
  /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("span", { children: "Consultar Benef\xEDcios" }),
@@ -472,9 +478,6 @@ function Loading({ textColor }) {
472
478
  }
473
479
  var Loading_default = Loading;
474
480
 
475
- // src/components/BenefitsTable/index.tsx
476
- var import_react4 = require("react");
477
-
478
481
  // src/components/UI/Title/index.tsx
479
482
  var import_classnames5 = __toESM(require("classnames"));
480
483
  var import_jsx_runtime7 = require("react/jsx-runtime");
@@ -495,6 +498,36 @@ function Title(props) {
495
498
  }
496
499
  var Title_default = Title;
497
500
 
501
+ // src/services/benefits-without-document.ts
502
+ var import_js_cookie2 = __toESM(require("js-cookie"));
503
+ var import_meta2 = {};
504
+ var CheckBenefistWithoutDocument = async ({ products }) => {
505
+ const API_URL = import_meta2.env.VITE_API_URL;
506
+ if (!API_URL) {
507
+ throw new Error("API URL is not defined in environment variables");
508
+ }
509
+ const AUTH_TOKEN = import_js_cookie2.default.get("pbm-token");
510
+ if (!AUTH_TOKEN) {
511
+ throw new Error("Token is not defined in cookies or is expired");
512
+ }
513
+ const response = await fetch(`${API_URL}/products/genericBenefit`, {
514
+ method: "POST",
515
+ headers: {
516
+ Authorization: `Bearer ${AUTH_TOKEN}`,
517
+ "Content-Type": "application/json"
518
+ },
519
+ body: JSON.stringify({ products })
520
+ });
521
+ const dataResponse = await response.json();
522
+ if (!dataResponse.success) {
523
+ throw new Error(dataResponse.message || "Failed to fetch benefits without document");
524
+ }
525
+ return dataResponse;
526
+ };
527
+
528
+ // src/components/BenefitsTable/index.tsx
529
+ var import_react4 = require("react");
530
+
498
531
  // src/libs/zustand/useModal.tsx
499
532
  var import_zustand2 = require("zustand");
500
533
  var initialModalState = {
@@ -566,7 +599,6 @@ var import_jsx_runtime9 = require("react/jsx-runtime");
566
599
  function Item({ data, onChange, checked }) {
567
600
  const { setAvailableDiscountSelected, state, isAuthenticatedShopper } = usePBMStore();
568
601
  const { setModal } = useModal();
569
- const [openModalAlert, setOpenModalAlert] = (0, import_react3.useState)(false);
570
602
  const ID_INPUT = "unity_quantity_" + data.authorizedQuantity;
571
603
  const decimalDiscount = data.discountPercentual / 100;
572
604
  const unitDiscountValue = data.grossPrice * decimalDiscount;
@@ -596,12 +628,15 @@ function Item({ data, onChange, checked }) {
596
628
  (0, import_react3.useEffect)(() => {
597
629
  updateStorageData();
598
630
  }, [updateStorageData]);
599
- const CheckUserIsAuthenticated = () => {
600
- if (isAuthenticatedShopper) return;
601
- setModal({
602
- id: "ShopperIsNotAuthenticated",
603
- open: true
604
- });
631
+ const handleChange = () => {
632
+ if (!isAuthenticatedShopper && state === "isActivated") {
633
+ setModal({
634
+ id: "ShopperIsNotAuthenticated",
635
+ open: true
636
+ });
637
+ return;
638
+ }
639
+ onChange();
605
640
  };
606
641
  return /* @__PURE__ */ (0, import_jsx_runtime9.jsxs)(
607
642
  "label",
@@ -612,7 +647,6 @@ function Item({ data, onChange, checked }) {
612
647
  { "cursor-not-allowed": state === "isPreview", "cursor-pointer": state === "isActivated" }
613
648
  ),
614
649
  id: "label_benefits_" + ID_INPUT,
615
- onClick: CheckUserIsAuthenticated,
616
650
  children: [
617
651
  /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(
618
652
  "input",
@@ -622,7 +656,7 @@ function Item({ data, onChange, checked }) {
622
656
  id: ID_INPUT,
623
657
  className: "hidden",
624
658
  checked,
625
- onChange,
659
+ onChange: handleChange,
626
660
  disabled: state === "isPreview"
627
661
  }
628
662
  ),
@@ -655,58 +689,31 @@ function Item({ data, onChange, checked }) {
655
689
  }
656
690
  var Item_default = Item;
657
691
 
658
- // src/services/benefits-without-document.ts
659
- var import_js_cookie2 = __toESM(require("js-cookie"));
660
- var import_meta2 = {};
661
- var CheckBenefistWithoutDocument = async ({ products }) => {
662
- const API_URL = import_meta2.env.VITE_API_URL;
663
- if (!API_URL) {
664
- throw new Error("API URL is not defined in environment variables");
665
- }
666
- const AUTH_TOKEN = import_js_cookie2.default.get("pbm-token");
667
- if (!AUTH_TOKEN) {
668
- throw new Error("Token is not defined in cookies or is expired");
669
- }
670
- const response = await fetch(`${API_URL}/products/genericBenefit`, {
671
- method: "POST",
672
- headers: {
673
- Authorization: `Bearer ${AUTH_TOKEN}`,
674
- "Content-Type": "application/json"
675
- },
676
- body: JSON.stringify({ products })
677
- });
678
- const dataResponse = await response.json();
679
- if (!dataResponse.success) {
680
- throw new Error(dataResponse.message || "Failed to fetch benefits without document");
681
- }
682
- return dataResponse;
683
- };
684
-
685
692
  // src/components/BenefitsTable/index.tsx
686
693
  var import_jsx_runtime10 = require("react/jsx-runtime");
687
694
  function BenefitsTable() {
695
+ const [isPending, startTransition] = (0, import_react4.useTransition)();
688
696
  const { securityNumber, setState, state, targetProduct, customLoginUrl, isAuthenticatedShopper } = usePBMStore();
689
697
  const [selectedDiscout, setSelectedDiscount] = (0, import_react4.useState)(null);
690
- const [loading, setLoading] = (0, import_react4.useState)(true);
691
698
  const [benefitsItems, setBenefitsItems] = (0, import_react4.useState)();
692
699
  (0, import_react4.useEffect)(() => {
700
+ if (!targetProduct?.productId) {
701
+ console.error("PBMLOG: Product ID is not defined on targetProduct");
702
+ return;
703
+ }
704
+ if (!targetProduct.ean) {
705
+ console.error("PBMLOG: EAN is not defined on targetProduct");
706
+ return;
707
+ }
708
+ if (!targetProduct.listPrice) {
709
+ console.error("PBMLOG: List Price is not defined on targetProduct");
710
+ return;
711
+ }
712
+ if (!targetProduct.price) {
713
+ console.error("PBMLOG: Price is not defined on targetProduct");
714
+ return;
715
+ }
693
716
  const fetchDicountsWithoutDocument = async () => {
694
- if (!targetProduct?.productId) {
695
- console.error("PBMLOG: Product ID is not defined on targetProduct");
696
- return;
697
- }
698
- if (!targetProduct.ean) {
699
- console.error("PBMLOG: EAN is not defined on targetProduct");
700
- return;
701
- }
702
- if (!targetProduct.listPrice) {
703
- console.error("PBMLOG: List Price is not defined on targetProduct");
704
- return;
705
- }
706
- if (!targetProduct.price) {
707
- console.error("PBMLOG: Price is not defined on targetProduct");
708
- return;
709
- }
710
717
  try {
711
718
  const data = {
712
719
  productId: Number(targetProduct.productId),
@@ -724,8 +731,6 @@ function BenefitsTable() {
724
731
  } catch (error) {
725
732
  setBenefitsItems(void 0);
726
733
  console.error(error);
727
- } finally {
728
- setLoading(false);
729
734
  }
730
735
  };
731
736
  const fetchDiscountWithDocument = async () => {
@@ -733,22 +738,6 @@ function BenefitsTable() {
733
738
  console.error("PBMLOG: Document is not defined");
734
739
  return;
735
740
  }
736
- if (!targetProduct?.productId) {
737
- console.error("PBMLOG: Product ID is not defined on targetProduct");
738
- return;
739
- }
740
- if (!targetProduct.ean) {
741
- console.error("PBMLOG: EAN is not defined on targetProduct");
742
- return;
743
- }
744
- if (!targetProduct.listPrice) {
745
- console.error("PBMLOG: List Price is not defined on targetProduct");
746
- return;
747
- }
748
- if (!targetProduct.price) {
749
- console.error("PBMLOG: Price is not defined on targetProduct");
750
- return;
751
- }
752
741
  try {
753
742
  const data = {
754
743
  productId: Number(targetProduct.productId),
@@ -766,13 +755,13 @@ function BenefitsTable() {
766
755
  } catch (error) {
767
756
  setBenefitsItems(void 0);
768
757
  console.error(error);
769
- } finally {
770
- setLoading(false);
771
758
  }
772
759
  };
773
- state === "isActivated" ? fetchDiscountWithDocument() : fetchDicountsWithoutDocument();
760
+ startTransition(async () => {
761
+ state === "isActivated" ? await fetchDiscountWithDocument() : await fetchDicountsWithoutDocument();
762
+ });
774
763
  }, []);
775
- if (loading) {
764
+ if (isPending) {
776
765
  return /* @__PURE__ */ (0, import_jsx_runtime10.jsxs)("main", { className: "flex items-center justify-center gap-4", id: "loading_pbm", children: [
777
766
  /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(
778
767
  "div",
@@ -804,6 +793,7 @@ function BenefitsTable() {
804
793
  {
805
794
  className: "flex flex-col items-center justify-start w-full gap-4.5",
806
795
  id: "form_benefits_table_pbm",
796
+ onSubmit: (e) => e.preventDefault(),
807
797
  children: [
808
798
  !benefitsItems && /* @__PURE__ */ (0, import_jsx_runtime10.jsx)("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." }),
809
799
  benefitsItems && benefitsItems.map((item, index) => {
@@ -930,22 +920,13 @@ function SecurityNumberInvalid({ textColor }) {
930
920
  /* @__PURE__ */ (0, import_jsx_runtime13.jsx)("br", {}),
931
921
  "Ao clicar em \u201CAceitar os termos\u201D, voc\xEA ir\xE1 para uma p\xE1gina externa. Aceite os termos e volte para continuar."
932
922
  ] }),
933
- /* @__PURE__ */ (0, import_jsx_runtime13.jsx)(
934
- Link_default,
935
- {
936
- href: urlAcceptTerms || "",
937
- children: "Aceitar os termos"
938
- }
939
- )
923
+ /* @__PURE__ */ (0, import_jsx_runtime13.jsx)(Link_default, { href: urlAcceptTerms || "", children: "Aceitar os termos" })
940
924
  ]
941
925
  }
942
926
  );
943
927
  }
944
928
  var SecurityNumberInvalid_default = SecurityNumberInvalid;
945
929
 
946
- // src/PBM.tsx
947
- var import_react5 = require("react");
948
-
949
930
  // src/components/SecurityNumberRegitered/index.tsx
950
931
  var import_jsx_runtime14 = require("react/jsx-runtime");
951
932
  function SecurityNumberRegitered({ textColor }) {
@@ -959,19 +940,87 @@ function SecurityNumberRegitered({ textColor }) {
959
940
  children: [
960
941
  /* @__PURE__ */ (0, import_jsx_runtime14.jsx)(Title_default, { className: "w-full", textColor, children: "Ops, seu CPF ainda n\xE3o est\xE1 habilitado para este produto." }),
961
942
  /* @__PURE__ */ (0, import_jsx_runtime14.jsx)(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." }),
962
- /* @__PURE__ */ (0, import_jsx_runtime14.jsx)(
963
- Link_default,
964
- {
965
- href: urlRegisterIndustry || "",
966
- children: "Ativar CPF"
967
- }
968
- )
943
+ /* @__PURE__ */ (0, import_jsx_runtime14.jsx)(Link_default, { href: urlRegisterIndustry || "", children: "Ativar CPF" })
969
944
  ]
970
945
  }
971
946
  );
972
947
  }
973
948
  var SecurityNumberRegitered_default = SecurityNumberRegitered;
974
949
 
950
+ // src/components/Errors/ErrorToApplyBenefits.tsx
951
+ var import_lucide_react2 = require("lucide-react");
952
+ var import_jsx_runtime15 = require("react/jsx-runtime");
953
+ var ErrorToApplyBenefits = () => {
954
+ return /* @__PURE__ */ (0, import_jsx_runtime15.jsxs)(Container_default, { variant: "main", children: [
955
+ /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(Title_default, { children: "Erro ao Aplicar o Benef\xEDcio" }),
956
+ /* @__PURE__ */ (0, import_jsx_runtime15.jsxs)(Text_default, { textAlign: "center", children: [
957
+ "O produto n\xE3o foi encontrado no sistema. ",
958
+ /* @__PURE__ */ (0, import_jsx_runtime15.jsx)("br", {}),
959
+ " Por favor, tente novamente mais tarde ou contate o suporte."
960
+ ] }),
961
+ /* @__PURE__ */ (0, import_jsx_runtime15.jsxs)(
962
+ Button_default,
963
+ {
964
+ 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",
965
+ onClick: () => window.location.reload(),
966
+ id: "check_benefits_button",
967
+ children: [
968
+ /* @__PURE__ */ (0, import_jsx_runtime15.jsx)("span", { children: "Tentar novamente" }),
969
+ /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(import_lucide_react2.RefreshCw, { size: 16 })
970
+ ]
971
+ }
972
+ )
973
+ ] });
974
+ };
975
+ var ErrorToApplyBenefits_default = ErrorToApplyBenefits;
976
+
977
+ // src/components/Modal/index.tsx
978
+ var import_classnames9 = __toESM(require("classnames"));
979
+ var import_jsx_runtime16 = require("react/jsx-runtime");
980
+ var Modal = ({ ID }) => {
981
+ const { customLoginUrl } = usePBMStore();
982
+ const { modal, setModal } = useModal();
983
+ return /* @__PURE__ */ (0, import_jsx_runtime16.jsxs)("main", { className: (0, import_classnames9.default)(
984
+ "fixed inset-0 flex items-center justify-center z-50 flex-col transition-all shadow bg-black/40",
985
+ {
986
+ "opacity-100 pointer-events-auto": modal.id === ID && modal.open,
987
+ "opacity-0 pointer-events-none": modal.id != ID || !modal.open
988
+ }
989
+ ), children: [
990
+ /* @__PURE__ */ (0, import_jsx_runtime16.jsx)("div", { className: "absolute inset-0", onClick: () => setModal({ id: "", open: false }) }),
991
+ /* @__PURE__ */ (0, import_jsx_runtime16.jsxs)("section", { className: "z-10 bg-white gap-2 flex-col items-center-safe justify-center-safe p-8 rounded-xl", children: [
992
+ /* @__PURE__ */ (0, import_jsx_runtime16.jsx)(Title_default, { textColor: "tomato", textSize: "18px", textAlign: "center", className: "mb-2", children: "Opa! Parece que voc\xEA n\xE3o est\xE1 Logado" }),
993
+ /* @__PURE__ */ (0, import_jsx_runtime16.jsxs)(Text_default, { className: "mb-2", textAlign: "center", children: [
994
+ "Para aproveitar os benef\xEDcios \xE9 necess\xE1rio realizar o ",
995
+ /* @__PURE__ */ (0, import_jsx_runtime16.jsx)("strong", { children: "Login" }),
996
+ " ou ",
997
+ /* @__PURE__ */ (0, import_jsx_runtime16.jsx)("strong", { children: "Cadastro" }),
998
+ " no site!"
999
+ ] }),
1000
+ /* @__PURE__ */ (0, import_jsx_runtime16.jsxs)("section", { className: "flex items-center-safe justify-center-safe gap-4", children: [
1001
+ /* @__PURE__ */ (0, import_jsx_runtime16.jsx)(Button_default, { className: "bg-gray-600 hover:bg-gray-500", onClick: () => setModal({ id: "", open: false }), children: "Seguir sem Benef\xEDcios" }),
1002
+ /* @__PURE__ */ (0, import_jsx_runtime16.jsx)(Link_default, { href: customLoginUrl || "/login", children: "Aproveitar Benef\xEDcios" })
1003
+ ] })
1004
+ ] })
1005
+ ] });
1006
+ };
1007
+ var Modal_default = Modal;
1008
+
1009
+ // src/PBM.tsx
1010
+ var import_Skeleton = __toESM(require("@mui/material/Skeleton"));
1011
+
1012
+ // src/utils/format-price.ts
1013
+ var formatedPrice = (price) => {
1014
+ const result = Number(String(price).replace(",", "."));
1015
+ return result;
1016
+ };
1017
+
1018
+ // src/PBM.tsx
1019
+ var import_react6 = require("react");
1020
+
1021
+ // src/hooks/useAppStartup.tsx
1022
+ var import_react5 = require("react");
1023
+
975
1024
  // src/services/authorization.ts
976
1025
  var import_js_cookie3 = __toESM(require("js-cookie"));
977
1026
  var GetAuthorization = async ({ clientID }) => {
@@ -1003,9 +1052,6 @@ var GetAuthorization = async ({ clientID }) => {
1003
1052
  return data;
1004
1053
  };
1005
1054
 
1006
- // src/PBM.tsx
1007
- var import_lucide_react2 = require("lucide-react");
1008
-
1009
1055
  // src/services/get-product-by-ean.ts
1010
1056
  var import_js_cookie4 = __toESM(require("js-cookie"));
1011
1057
  var import_meta3 = {};
@@ -1029,41 +1075,6 @@ var GetProductByEAN = async ({ PRODUCT_EAN }) => {
1029
1075
  return dataResponse;
1030
1076
  };
1031
1077
 
1032
- // src/PBM.tsx
1033
- var import_Skeleton = __toESM(require("@mui/material/Skeleton"));
1034
-
1035
- // src/components/Modal/index.tsx
1036
- var import_classnames9 = __toESM(require("classnames"));
1037
- var import_jsx_runtime15 = require("react/jsx-runtime");
1038
- var Modal = ({ ID }) => {
1039
- const { customLoginUrl } = usePBMStore();
1040
- const { modal, setModal } = useModal();
1041
- return /* @__PURE__ */ (0, import_jsx_runtime15.jsxs)("main", { className: (0, import_classnames9.default)(
1042
- "fixed inset-0 flex items-center justify-center z-50 flex-col transition-all shadow bg-black/40",
1043
- {
1044
- "opacity-100 pointer-events-auto": modal.id === ID && modal.open,
1045
- "opacity-0 pointer-events-none": modal.id != ID || !modal.open
1046
- }
1047
- ), children: [
1048
- /* @__PURE__ */ (0, import_jsx_runtime15.jsx)("div", { className: "absolute inset-0", onClick: () => setModal({ id: "", open: false }) }),
1049
- /* @__PURE__ */ (0, import_jsx_runtime15.jsxs)("section", { className: "z-10 bg-white gap-2 flex-col items-center-safe justify-center-safe p-8 rounded-xl", children: [
1050
- /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(Title_default, { textColor: "tomato", textSize: "18px", textAlign: "center", className: "mb-2", children: "Opa! Parece que voc\xEA n\xE3o est\xE1 Logado" }),
1051
- /* @__PURE__ */ (0, import_jsx_runtime15.jsxs)(Text_default, { className: "mb-2", textAlign: "center", children: [
1052
- "Para aproveitar os benef\xEDcios \xE9 necess\xE1rio realizar o ",
1053
- /* @__PURE__ */ (0, import_jsx_runtime15.jsx)("strong", { children: "Login" }),
1054
- " ou ",
1055
- /* @__PURE__ */ (0, import_jsx_runtime15.jsx)("strong", { children: "Cadastro" }),
1056
- " no site!"
1057
- ] }),
1058
- /* @__PURE__ */ (0, import_jsx_runtime15.jsxs)("section", { className: "flex items-center-safe justify-center-safe gap-4", children: [
1059
- /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(Button_default, { className: "bg-gray-600 hover:bg-gray-500", onClick: () => setModal({ id: "", open: false }), children: "Seguir sem Benef\xEDcios" }),
1060
- /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(Link_default, { href: customLoginUrl || "/login", children: "Aproveitar Benef\xEDcios" })
1061
- ] })
1062
- ] })
1063
- ] });
1064
- };
1065
- var Modal_default = Modal;
1066
-
1067
1078
  // src/services/get-list-products.ts
1068
1079
  var import_js_cookie5 = __toESM(require("js-cookie"));
1069
1080
  var import_meta4 = {};
@@ -1087,44 +1098,33 @@ var GetProductsWithBenefits = async () => {
1087
1098
  return dataResponse;
1088
1099
  };
1089
1100
 
1090
- // src/PBM.tsx
1091
- var import_jsx_runtime16 = require("react/jsx-runtime");
1092
- function PBM({
1093
- originalProductPrice,
1094
- clientID,
1095
- eanProduct,
1096
- isAuthenticatedShopper,
1097
- customLoginUrl
1098
- }) {
1099
- const formatedOriginalProductPrice = Number(
1100
- String(originalProductPrice).replace(",", ".")
1101
- );
1102
- const [isReady, setIsReady] = (0, import_react5.useState)(false);
1103
- const [loading, setLoading] = (0, import_react5.useState)(false);
1104
- const { state, setTargetProduct, targetProduct, setIsAuthenticatedShopper, setCustomLoginUrl } = usePBMStore();
1105
- const [EanProductExist, setEanProductExist] = (0, import_react5.useState)(true);
1101
+ // src/hooks/useAppStartup.tsx
1102
+ var useAppStartup = (props) => {
1103
+ const { setTargetProduct, targetProduct, setIsAuthenticatedShopper, setCustomLoginUrl } = usePBMStore();
1104
+ const [IsReady, setIsReady] = (0, import_react5.useState)(false);
1106
1105
  const [IsValid, setIsValid] = (0, import_react5.useState)(false);
1106
+ const [EanProductExist, setEanProductExist] = (0, import_react5.useState)(true);
1107
1107
  const fetchValidateProduct = (0, import_react5.useCallback)(async () => {
1108
1108
  try {
1109
1109
  const response = await GetProductsWithBenefits();
1110
1110
  if (response.success) {
1111
1111
  const data = response.data;
1112
- const hasProductInList = data.some((product) => product.ean == eanProduct);
1112
+ const hasProductInList = data.some((product) => product.ean == props.eanProduct);
1113
1113
  setIsValid(hasProductInList);
1114
1114
  }
1115
1115
  } catch (error) {
1116
1116
  console.error(error);
1117
1117
  }
1118
- }, [eanProduct, IsValid]);
1118
+ }, [props.eanProduct, IsValid]);
1119
1119
  const fetchProductByEan = async () => {
1120
1120
  try {
1121
1121
  if (!IsValid) return;
1122
- if (!eanProduct) {
1122
+ if (!props.eanProduct) {
1123
1123
  console.error("PBMLOG: Ean is not defined.");
1124
1124
  setEanProductExist(false);
1125
1125
  return;
1126
1126
  }
1127
- const response = await GetProductByEAN({ PRODUCT_EAN: eanProduct });
1127
+ const response = await GetProductByEAN({ PRODUCT_EAN: props.eanProduct });
1128
1128
  if (response.success && response.data) {
1129
1129
  setEanProductExist(true);
1130
1130
  const { pbm, sku, ...targetProductNewData } = response.data;
@@ -1135,10 +1135,10 @@ function PBM({
1135
1135
  informativeMessage: pbm.informativeMessage ?? "",
1136
1136
  discountMax: pbm.discountMax ?? 0,
1137
1137
  industryLogo: pbm.imageLink ?? void 0,
1138
- ean: eanProduct
1138
+ ean: props.eanProduct
1139
1139
  });
1140
- setIsAuthenticatedShopper(isAuthenticatedShopper);
1141
- setCustomLoginUrl(customLoginUrl);
1140
+ setIsAuthenticatedShopper(props.isAuthenticatedShopper);
1141
+ setCustomLoginUrl(props.customLoginUrl);
1142
1142
  }
1143
1143
  } catch (error) {
1144
1144
  console.error(error);
@@ -1146,9 +1146,9 @@ function PBM({
1146
1146
  setIsReady(true);
1147
1147
  }
1148
1148
  };
1149
- const handleAuthorizationRequest = (0, import_react5.useCallback)(async () => {
1149
+ const fetchAuthorizationRequest = (0, import_react5.useCallback)(async () => {
1150
1150
  try {
1151
- const response = await GetAuthorization({ clientID });
1151
+ const response = await GetAuthorization({ clientID: props.clientID });
1152
1152
  if (response.success) {
1153
1153
  fetchValidateProduct();
1154
1154
  fetchProductByEan();
@@ -1158,56 +1158,40 @@ function PBM({
1158
1158
  } catch (error) {
1159
1159
  console.error("Error fetching authorization:", error);
1160
1160
  }
1161
- }, [clientID, IsValid]);
1161
+ }, [props.clientID, IsValid]);
1162
1162
  (0, import_react5.useEffect)(() => {
1163
- handleAuthorizationRequest();
1164
- }, [handleAuthorizationRequest]);
1165
- if (!isReady) {
1166
- return /* @__PURE__ */ (0, import_jsx_runtime16.jsx)(import_Skeleton.default, { variant: "rectangular", style: { borderRadius: "12px" }, width: 460, height: 358 });
1163
+ fetchAuthorizationRequest();
1164
+ }, [fetchAuthorizationRequest]);
1165
+ return { IsReady, IsValid, EanProductExist };
1166
+ };
1167
+ var useAppStartup_default = useAppStartup;
1168
+
1169
+ // src/PBM.tsx
1170
+ var import_jsx_runtime17 = require("react/jsx-runtime");
1171
+ function PBM(props) {
1172
+ const { state } = usePBMStore();
1173
+ const { IsReady, EanProductExist, IsValid } = useAppStartup_default(props);
1174
+ const [isPending, startTransition] = (0, import_react6.useTransition)();
1175
+ const formatedProductPrice = formatedPrice(props.originalProductPrice);
1176
+ if (!IsReady) {
1177
+ return /* @__PURE__ */ (0, import_jsx_runtime17.jsx)(import_Skeleton.default, { variant: "rectangular", style: { borderRadius: "12px" }, width: 460, height: 358 });
1167
1178
  }
1168
1179
  if (!EanProductExist) {
1169
- return /* @__PURE__ */ (0, import_jsx_runtime16.jsx)("div", { id: "pbm-library-root", children: /* @__PURE__ */ (0, import_jsx_runtime16.jsxs)(Container_default, { variant: "main", children: [
1170
- /* @__PURE__ */ (0, import_jsx_runtime16.jsx)(Title_default, { children: "Erro ao Aplicar o Benef\xEDcio" }),
1171
- /* @__PURE__ */ (0, import_jsx_runtime16.jsxs)(Text_default, { textAlign: "center", children: [
1172
- "O produto n\xE3o foi encontrado no sistema. ",
1173
- /* @__PURE__ */ (0, import_jsx_runtime16.jsx)("br", {}),
1174
- " Por favor, tente novamente mais tarde ou contate o suporte."
1175
- ] }),
1176
- /* @__PURE__ */ (0, import_jsx_runtime16.jsxs)(
1177
- Button_default,
1178
- {
1179
- 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",
1180
- onClick: () => window.location.reload(),
1181
- id: "check_benefits_button",
1182
- children: [
1183
- /* @__PURE__ */ (0, import_jsx_runtime16.jsx)("span", { children: "Tentar novamente" }),
1184
- /* @__PURE__ */ (0, import_jsx_runtime16.jsx)(import_lucide_react2.RefreshCw, { size: 16 })
1185
- ]
1186
- }
1187
- )
1188
- ] }) });
1180
+ return /* @__PURE__ */ (0, import_jsx_runtime17.jsx)("div", { id: "pbm-library-root", children: /* @__PURE__ */ (0, import_jsx_runtime17.jsx)(ErrorToApplyBenefits_default, {}) });
1189
1181
  }
1190
- if (!IsValid) {
1191
- return;
1192
- }
1193
- const RenderStateComponent = () => {
1194
- const StateMap = {
1195
- "isEmpty": /* @__PURE__ */ (0, import_jsx_runtime16.jsx)(Form_default, { setLoading }),
1196
- "isInvalid": /* @__PURE__ */ (0, import_jsx_runtime16.jsx)(SecurityNumberInvalid_default, {}),
1197
- "isRegistered": /* @__PURE__ */ (0, import_jsx_runtime16.jsx)(SecurityNumberRegitered_default, {}),
1198
- "isActivated": /* @__PURE__ */ (0, import_jsx_runtime16.jsx)(BenefitsTable_default, {}),
1199
- "isPreview": /* @__PURE__ */ (0, import_jsx_runtime16.jsx)(BenefitsTable_default, {})
1200
- };
1201
- if (!loading) return StateMap[state];
1202
- else return /* @__PURE__ */ (0, import_jsx_runtime16.jsx)(Loading_default, {});
1203
- };
1204
- return /* @__PURE__ */ (0, import_jsx_runtime16.jsxs)("div", { id: "pbm-library-root", children: [
1205
- /* @__PURE__ */ (0, import_jsx_runtime16.jsxs)(Container_default, { variant: "main", children: [
1206
- /* @__PURE__ */ (0, import_jsx_runtime16.jsx)(Header_default, { originalProductPrice: formatedOriginalProductPrice || 0 }),
1207
- /* @__PURE__ */ (0, import_jsx_runtime16.jsx)(Container_default, { variant: "simple", children: /* @__PURE__ */ (0, import_jsx_runtime16.jsx)(RenderStateComponent, {}) }),
1208
- /* @__PURE__ */ (0, import_jsx_runtime16.jsx)(Footer_default, {})
1182
+ if (!IsValid) return;
1183
+ return /* @__PURE__ */ (0, import_jsx_runtime17.jsxs)("div", { id: "pbm-library-root", children: [
1184
+ /* @__PURE__ */ (0, import_jsx_runtime17.jsxs)(Container_default, { variant: "main", children: [
1185
+ /* @__PURE__ */ (0, import_jsx_runtime17.jsx)(Header_default, { originalProductPrice: formatedProductPrice || 0 }),
1186
+ /* @__PURE__ */ (0, import_jsx_runtime17.jsx)(Container_default, { variant: "simple", children: isPending ? /* @__PURE__ */ (0, import_jsx_runtime17.jsx)(Loading_default, {}) : /* @__PURE__ */ (0, import_jsx_runtime17.jsxs)(import_jsx_runtime17.Fragment, { children: [
1187
+ state === "isEmpty" && /* @__PURE__ */ (0, import_jsx_runtime17.jsx)(Form_default, { startTransition }),
1188
+ state === "isInvalid" && /* @__PURE__ */ (0, import_jsx_runtime17.jsx)(SecurityNumberInvalid_default, {}),
1189
+ state === "isRegistered" && /* @__PURE__ */ (0, import_jsx_runtime17.jsx)(SecurityNumberRegitered_default, {}),
1190
+ (state === "isActivated" || state === "isPreview") && /* @__PURE__ */ (0, import_jsx_runtime17.jsx)(BenefitsTable_default, {})
1191
+ ] }) }),
1192
+ /* @__PURE__ */ (0, import_jsx_runtime17.jsx)(Footer_default, {})
1209
1193
  ] }),
1210
- /* @__PURE__ */ (0, import_jsx_runtime16.jsx)(Modal_default, { ID: "ShopperIsNotAuthenticated" })
1194
+ /* @__PURE__ */ (0, import_jsx_runtime17.jsx)(Modal_default, { ID: "ShopperIsNotAuthenticated" })
1211
1195
  ] });
1212
1196
  }
1213
1197
  var PBM_default = PBM;