import{q as c,n as e}from"./vendor-react-Br0ZiotI.js";import{b_ as A,cv as H,bX as y,bM as K,bZ as L,D as T,z as R}from"./index-BdS0v754.js";import"./vendor-dayjs-8pzXSYYG.js";import{u as Y,T as W}from"./vendor-i18n-C54Hc0CQ.js";import{D as B}from"./download-CrMEAWCV.js";import{Z}from"./zap-Ci_2NnzB.js";import"./vendor-query-9i1Av1pr.js";import"./vendor-antd-core-CbYNMZQB.js";import"./vendor-sentry-Cx26FE6p.js";/**
 * @license lucide-react v1.16.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const z=[["rect",{width:"14",height:"20",x:"5",y:"2",rx:"2",ry:"2",key:"1yt0o3"}],["path",{d:"M12 18h.01",key:"mhygvu"}]],X=A("smartphone",z),C="avalog:pwa:dismissedAt",h="avalog:pwa:visits",g="avalog:pwa:totalSeconds",p="avalog:pwa:lkVisits",O="avalog:pwa:sessionMarked",q=8,F=300,G=5,J=4,Q=6e4,U=45,$=U*864e5,tt=1e4;function n(t,r=localStorage){try{const o=r.getItem(t);return o&&Number(o)||0}catch{return 0}}function w(t,r,o=localStorage){try{o.setItem(t,String(r))}catch{}}function et(){var t;return((t=window.matchMedia)==null?void 0:t.call(window,"(display-mode: standalone)").matches)||window.navigator.standalone===!0}function st(){return/iPhone|iPad|iPod/i.test(navigator.userAgent)&&!/Mac/i.test(navigator.vendor)}function at(){const t=n(C);return t>0&&Date.now()-t<$}function nt(){try{return localStorage.getItem("isAuthenticated")==="true"}catch{return!1}}function rt(){return window.location.pathname.startsWith("/lk")}function it(){return/^\/(checkout|payment|pay|order|cart)/i.test(window.location.pathname)}function _(){w(C,Date.now())}function ot(){try{if(sessionStorage.getItem(O)==="1")return n(h);sessionStorage.setItem(O,"1")}catch{}const t=n(h)+1;return w(h,t),t}function lt(){if(!window.location.pathname.startsWith("/lk"))return n(p);const t=n(p)+1;return w(p,t),t}const ct=t=>[{icon:e.jsx(Z,{className:"w-4 h-4 text-yellow-500"}),text:t("pwa.install.featureLaunch","Быстрый запуск с экрана")},{icon:e.jsx(R,{className:"w-4 h-4 text-blue-500"}),text:t("pwa.install.featurePush","Push-уведомления о грузе")},{icon:e.jsx(X,{className:"w-4 h-4 text-indigo-500"}),text:t("pwa.install.featureNoBrowser","Работает без браузера")}];function St(){const{t}=Y(),[r,o]=c.useState(null),[M,x]=c.useState(!1),[v,V]=c.useState(!1),u=c.useRef(null),l=H("pwa_install_prompt_v2",["a","b"]);c.useEffect(()=>{if(et()||at()||!nt()||it())return;const a=ot(),S=lt(),b=Date.now();let I=!1;const d=()=>{if(I)return;I=!0;const s=Math.round((Date.now()-b)/1e3),i=n(g)+s;w(g,i)},N=()=>{document.visibilityState==="hidden"&&d()};window.addEventListener("pagehide",d),document.addEventListener("visibilitychange",N);const m=(s,i)=>u.current?!0:rt()?(u.current=s,x(!0),y("pwa_install_prompt_shown",{trigger:s,mode:i,variant:l,visits:n(h),total_seconds:n(g),lk_visits:n(p)}),!0):!1,D=s=>{if(l==="a")return a>=J&&Date.now()-b>=Q?m("visits",s):!1;if(a>=q)return m("visits",s);if(S>=G)return m("lk_visits",s);const i=Math.round((Date.now()-b)/1e3);return n(g)+i>=F?m("time",s):!1},E=s=>{if(D(s))return null;const i=window.setInterval(()=>{D(s)&&window.clearInterval(i)},tt);return i};let f=null;const j=s=>{s.preventDefault(),o(s),f=E("native")};return window.addEventListener("beforeinstallprompt",j),st()&&(V(!0),f=E("ios")),()=>{window.removeEventListener("beforeinstallprompt",j),window.removeEventListener("pagehide",d),document.removeEventListener("visibilitychange",N),f!==null&&window.clearInterval(f),d()}},[l]);const P=c.useCallback(async()=>{if(r){try{await r.prompt();const a=await r.userChoice;y(a.outcome==="accepted"?"pwa_install_accepted":"pwa_install_dismissed",{trigger:u.current,variant:l,source:"native"}),a.outcome==="dismissed"&&_()}catch{_()}x(!1),o(null)}},[r,l]),k=c.useCallback(()=>{_(),x(!1),y("pwa_install_dismissed",{trigger:u.current,variant:l,source:v?"ios_guide":"ui"})},[l,v]);return M?e.jsxs("div",{className:L("fixed bottom-4 left-4 right-4 z-[9990] max-w-sm mx-auto","rounded-2xl bg-white dark:bg-gray-900 shadow-2xl border border-gray-100 dark:border-gray-700","p-5 animate-in slide-in-from-bottom-4 duration-300"),role:"dialog","aria-label":t("pwa.install.aria","Установить приложение"),children:[e.jsx("button",{type:"button",onClick:k,className:L("absolute top-2.5 right-2.5 inline-flex items-center justify-center","w-9 h-9 rounded-full","bg-gray-100 dark:bg-gray-800 text-gray-600 dark:text-gray-200","hover:bg-gray-200 dark:hover:bg-gray-700 hover:text-gray-900 dark:hover:text-white","focus:outline-none focus-visible:ring-2 focus-visible:ring-blue-500 transition-colors"),"aria-label":t("pwa.install.closeAria","Закрыть"),children:e.jsx(K,{className:"w-5 h-5"})}),e.jsxs("div",{className:"flex items-center gap-3 mb-4",children:[e.jsx("div",{className:"w-12 h-12 rounded-xl bg-blue-600 flex items-center justify-center flex-shrink-0",children:e.jsx("img",{src:"/favicon.svg",alt:"Avalog",className:"w-8 h-8",onError:a=>{a.target.style.display="none"}})}),e.jsxs("div",{children:[e.jsx("p",{className:"font-bold text-gray-900 dark:text-white text-sm",children:"Avalog"}),e.jsx("p",{className:"text-xs text-gray-500",children:t("pwa.install.subtitle","Установить как приложение")})]})]}),e.jsx("ul",{className:"space-y-2 mb-4",children:ct(t).map((a,S)=>e.jsxs("li",{className:"flex items-center gap-2 text-sm text-gray-700 dark:text-gray-300",children:[a.icon,a.text]},S))}),v?e.jsx("div",{className:"rounded-xl bg-gray-50 dark:bg-gray-800 p-3 text-xs text-gray-600 dark:text-gray-300 leading-relaxed",children:e.jsx(W,{i18nKey:"pwa.install.iosGuide",defaults:'Нажмите <s>↗ Поделиться</s> в Safari → <s>«На экран "Домой"»</s>',components:{s:e.jsx("strong",{})}})}):e.jsxs("div",{className:"flex gap-2",children:[e.jsxs(T,{className:"flex-1 gap-2",onClick:P,children:[e.jsx(B,{className:"w-4 h-4"}),t("pwa.install.installCta","Установить")]}),e.jsx(T,{variant:"ghost",onClick:k,className:"flex-shrink-0",children:t("pwa.install.notNow","Не сейчас")})]})]}):null}export{St as PwaInstallPromptDS};