{"version":3,"file":"pages.cjs","names":["defaultProps","validationPropsPgBar","RzlProgress","isSameURLWithoutSearch","isSameURL"],"sources":["../../src/progress-bar/pages.ts"],"sourcesContent":["import { memo, useCallback, useEffect, useRef } from \"react\";\n\nimport Router from \"next/router\";\nimport { isBoolean } from \"@rzl-zone/utils-js/predicates\";\nimport {\n  disableUserInteraction,\n  enableUserInteraction\n} from \"@rzl-zone/utils-js/events\";\n\nimport { RzlProgress } from \"./utils/rzlProgress\";\nimport { useCssTopLoader } from \"./hooks/useCssTopLoader\";\nimport { validationPropsPgBar } from \"./utils/validation\";\nimport { isSameURL, isSameURLWithoutSearch } from \"./utils/pathURL\";\nimport { defaultPropsInitRzlNextProgressBar as defaultProps } from \"./constants\";\n\nimport type { ProgressBarPagesComponentProps } from \"./types\";\n\nconst ProgressBarPagesComponent = (\n  props: ProgressBarPagesComponentProps = defaultProps\n): null => {\n  const timer = useRef<NodeJS.Timeout | number>(0);\n\n  const {\n    id,\n    name,\n    nonce,\n    style,\n    colorSpinner,\n    classNameIfLoading,\n    spinnerSpeed,\n    spinnerSize,\n    spinnerEase,\n    color,\n    height,\n    zIndex,\n    showAtBottom,\n    startPosition,\n    delay,\n    stopDelay,\n    options,\n\n    disableSameURL,\n    shallowRouting\n  } = validationPropsPgBar(props);\n\n  useCssTopLoader({\n    id,\n    name,\n    nonce,\n    style,\n    color,\n    height,\n    colorSpinner,\n    spinnerEase,\n    zIndex,\n    spinnerSize,\n    spinnerSpeed,\n    showAtBottom\n  });\n\n  const startProgress = useCallback(\n    (withDelay = true) => {\n      if (!isBoolean(withDelay)) withDelay = true;\n      if (RzlProgress.isStarted() || RzlProgress.isRendered()) return;\n\n      RzlProgress.configure(options);\n\n      if (timer.current) clearTimeout(timer.current);\n      timer.current = setTimeout(\n        () => {\n          if (startPosition > 0) RzlProgress.set(startPosition);\n\n          RzlProgress.start();\n          disableUserInteraction(classNameIfLoading);\n        },\n        withDelay ? delay : 1\n      );\n    },\n    [options]\n  );\n\n  const stopProgress = useCallback(\n    (\n      /**\n       * @default true\n       */\n      withDelay = true,\n      /**\n       * @default false\n       */\n      force?: boolean\n    ) => {\n      if (!isBoolean(withDelay)) withDelay = true;\n      if (!isBoolean(force)) force = false;\n\n      if (timer.current) clearTimeout(timer.current);\n\n      timer.current = setTimeout(\n        () => {\n          enableUserInteraction(classNameIfLoading);\n\n          if (!(RzlProgress.isStarted() || RzlProgress.isRendered())) return;\n\n          RzlProgress.done(force);\n        },\n        withDelay ? stopDelay : 1\n      );\n\n      RzlProgress.configure(options);\n    },\n    [options]\n  );\n\n  useEffect(() => {\n    const handleRouteStart = (url: string) => {\n      const targetUrl = new URL(url, location.href);\n      const currentUrl = new URL(location.href);\n\n      if (\n        shallowRouting &&\n        isSameURLWithoutSearch(targetUrl, currentUrl) &&\n        disableSameURL\n      )\n        return;\n\n      // If the URL is the same, we don't want to start the progress bar\n      if (isSameURL(targetUrl, currentUrl) && disableSameURL) return;\n\n      startProgress();\n    };\n\n    const handleRouteDone = () => {\n      stopProgress();\n    };\n\n    Router.events.on(\"routeChangeStart\", handleRouteStart);\n    Router.events.on(\"routeChangeComplete\", handleRouteDone);\n    Router.events.on(\"routeChangeError\", handleRouteDone);\n\n    return () => {\n      if (timer.current) clearTimeout(timer.current);\n\n      // Make sure to remove the event handler on unmount!\n      Router.events.off(\"routeChangeStart\", handleRouteStart);\n      Router.events.off(\"routeChangeComplete\", handleRouteDone);\n      Router.events.off(\"routeChangeError\", handleRouteDone);\n    };\n  }, []);\n\n  return null;\n};\n\n/** ------------------------------------------------------------------\n * * ***Component: `RzlNextPagesProgressBar`.***\n * ------------------------------------------------------------------\n * **A lightweight progress bar optimized for the Next.js `Pages Router`.**\n *\n * This component memoizes using `React.memo` to prevent unnecessary re-renders and\n * improve performance during client-side navigation events.\n *\n * - **Features**:\n *    - Provides a smooth progress bar for **Pages Directory** apps.\n *    - Automatically reacts to route changes via the Pages Router.\n *    - Improved performance thanks to `React.memo` optimization.\n *    - Works seamlessly with dynamic routes, `next/link`, and `router.push`.\n *\n * @returns {null} This component renders nothing; it only attaches route progress logic **(side-effect only)**.\n *\n * @example\n * * Basic usage inside `_app.tsx`:\n * ```tsx\n * import type { AppProps } from \"next/app\";\n * import { RzlNextPagesProgressBar } from \"@rzl-zone/next-kit/progress-bar/pages\";\n *\n * function MyApp({ Component, pageProps }: AppProps) {\n *   return (\n *     <>\n *       <RzlNextPagesProgressBar />\n *       <Component {...pageProps} />\n *     </>\n *   );\n * }\n *\n * export default MyApp;\n * ```\n */\nexport const RzlNextPagesProgressBar = memo(ProgressBarPagesComponent);\n\nRzlNextPagesProgressBar.displayName = `RzlNextPagesProgressBar(${\n  RzlNextPagesProgressBar.displayName ??\n  RzlNextPagesProgressBar.name ??\n  \"Component\"\n})`;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAiBA,MAAM,6BACJ,QAAwCA,2DAC/B;CACT,MAAM,0BAAwC,CAAC;CAE/C,MAAM,EACJ,IACA,MACA,OACA,OACA,cACA,oBACA,cACA,aACA,aACA,OACA,QACA,QACA,cACA,eACA,OACA,WACA,SAEA,gBACA,mBACEC,qCAAqB,KAAK;CAE9B,gCAAgB;EACd;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;CACF,CAAC;CAED,MAAM,wCACH,YAAY,SAAS;EACpB,IAAI,8CAAW,SAAS,GAAG,YAAY;EACvC,IAAIC,gCAAY,UAAU,KAAKA,gCAAY,WAAW,GAAG;EAEzD,gCAAY,UAAU,OAAO;EAE7B,IAAI,MAAM,SAAS,aAAa,MAAM,OAAO;EAC7C,MAAM,UAAU,iBACR;GACJ,IAAI,gBAAgB,GAAG,gCAAY,IAAI,aAAa;GAEpD,gCAAY,MAAM;GAClB,sDAAuB,kBAAkB;EAC3C,GACA,YAAY,QAAQ,CACtB;CACF,GACA,CAAC,OAAO,CACV;CAEA,MAAM,uCAKF,YAAY,MAIZ,UACG;EACH,IAAI,8CAAW,SAAS,GAAG,YAAY;EACvC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