"use strict";const taro=require("@tarojs/taro"),react=require("react"),_extends=require("@min-kit/extends"),shared=require("@min-kit/shared");function useMemoizedFn(e){const t=react.useRef(e);t.current=e;const n=react.useRef();return n.current||(n.current=function(...o){return t.current.apply(this,o)}),n.current}function createEventListener(e,t=taro.eventCenter){return function(n){const o=useMemoizedFn(n);react.useEffect(()=>(t.on(e,o),()=>{t.off(e,o)}),[])}}const useAppHide=createEventListener("onAppHide",{on:(e,t)=>taro.onAppHide(t),off:(e,t)=>taro.offAppHide(t)});try{if(typeof wx>"u"||typeof wx.onAppRoute!="function")throw new Error("Not support wx.onAppRoute");wx.onAppRoute(e=>{taro.eventCenter.trigger("wx.onAppRoute",e)}),_extends.logger.debug("#useAppRoute","delegated wx.onAppRoute")}catch(e){_extends.logger.warn("#useAppRoute",e)}const useAppRoute__unstable=createEventListener("wx.onAppRoute");try{if(typeof wx>"u"||typeof wx.onAppRouteDone!="function")throw new Error("Not support wx.onAppRouteDone");wx.onAppRouteDone(e=>{taro.eventCenter.trigger("wx.onAppRouteDone",e)}),_extends.logger.debug("#useAppRouteDone","delegated wx.onAppRouteDone")}catch(e){_extends.logger.warn("#useAppRouteDone",e)}const useAppRouteDone__unstable=createEventListener("wx.onAppRouteDone"),useAppShow=createEventListener("onAppHide",{on:(e,t)=>taro.onAppShow(t),off:(e,t)=>taro.offAppShow(t)});function useAppScene(){const[e,t]=react.useState(_extends.scene.current);return useAppShow(n=>{t(n.scene??_extends.scene.current)}),e}const useUpdateEffect=(e,t)=>{const n=react.useRef(!1);react.useEffect(()=>{if(!n.current)n.current=!0;else return e()},t)};function useTrue(e){const[t,n]=react.useState(!1);return useUpdateEffect(()=>{e===!0&&n(!0)},[e]),t}function useFalse(e){const[t,n]=react.useState(!0);return useUpdateEffect(()=>{e===!1&&n(!1)},[e]),t}function useLatest(e){const t=react.useRef(e);return t.current=e,t}const a=e=>{if(!e)return 0;const t=+e-Date.now();return t<0?0:t},h=e=>({days:Math.floor(e/864e5),hours:Math.floor(e/36e5)%24,minutes:Math.floor(e/6e4)%60,seconds:Math.floor(e/1e3)%60,milliseconds:Math.floor(e)%1e3});function useCountdown(e={}){const{leftTime:t,targetDate:n,interval:o=1e3,onEnd:u}=e,r=react.useMemo(()=>t&&t>0?Date.now()+t:n,[t,n]),[s,p]=react.useState(()=>a(r)),f=useLatest(u);react.useEffect(()=>{if(!r){p(0);return}p(a(r));const c=setInterval(()=>{const l=a(r);p(l),l===0&&(clearInterval(c),f.current?.())},o);return()=>clearInterval(c)},[r,o]);const i=react.useMemo(()=>h(s),[s]);return[s,i]}function useDidScan(e){taro.useDidShow(t=>{const n=t?.query;n?.q&&n?.scancode_time&&e(t)})}function useInterval(e,t){const n=useMemoizedFn(e),o=react.useRef(null),u=react.useCallback(()=>{o.current&&clearInterval(o.current)},[]);return react.useEffect(()=>(t>0&&(o.current=setInterval(n,t)),u),[t]),u}function useMap(){const[e,t]=react.useState(new Map);return[e,n=>{t(o=>{const u=new Map(o);return n(u),u})}]}function useMount(e){const t=react.useRef(e);react.useEffect(()=>{t.current()},[])}function useQuery(e){const[t,n]=react.useState(!1),[o,u]=react.useState(!1),[r,s]=react.useState(),[p,f]=react.useState();return{query:useMemoizedFn(async(...i)=>{if(!t){n(!0);try{const c=await e(...i);s(c),f(void 0)}catch(c){s(void 0),f(c)}finally{n(!1),u(!0)}}}),loading:t,loaded:o,data:r,error:p}}function useRouter(){const e=taro.useRouter();return react.useMemo(()=>{const t=Object.entries(e.params).reduce((n,[o,u])=>(n[o]=u&&decodeURIComponent(u),n),{});return{...e,params:Object.assign(t,{utm:shared.UTM.parse(t)})}},[e])}function useRouterParams(){const{params:e}=useRouter();return e}function useSet(e){const[t,n]=react.useState(new Set(e));return[t,o=>{n(u=>{const r=new Set(u);return o(r),r})}]}function useStorage(e,t){const[n,o]=react.useState(t?.defaultValue),u=useMemoizedFn(r=>{const s=typeof r=="function"?r(n):r;t?.onlyUpdate!=="storage"&&o(s),t?.onlyUpdate!=="state"&&_extends.setStorage(e,s)});return react.useEffect(()=>{_extends.getStorage(e).then(r=>{r!==void 0&&o(r)})},[e]),[n,u]}function useTaroId(){const e=react.useId();return react.useMemo(()=>"taro"+e,[e])}function useUnmount(e){const t=useLatest(e);react.useEffect(()=>()=>t.current(),[])}function useThrottleFn(e,{wait:t=1e3,...n}={}){const o=react.useMemo(()=>shared.throttle(e,t,n),[e]);return useUnmount(()=>{o.cancel()}),o}function useToggle(e){const[t,n]=react.useState(e),o=react.useRef(u=>{n(u!==void 0?u:r=>!r)});return[t,o.current]}function useUpdate(){const[,e]=react.useState({});return react.useCallback(()=>e({}),[])}exports.createEventListener=createEventListener,exports.useAppHide=useAppHide,exports.useAppRouteDone__unstable=useAppRouteDone__unstable,exports.useAppRoute__unstable=useAppRoute__unstable,exports.useAppScene=useAppScene,exports.useAppShow=useAppShow,exports.useCountdown=useCountdown,exports.useDidScan=useDidScan,exports.useFalse=useFalse,exports.useInterval=useInterval,exports.useLatest=useLatest,exports.useMap=useMap,exports.useMemoizedFn=useMemoizedFn,exports.useMount=useMount,exports.useQuery=useQuery,exports.useRouter=useRouter,exports.useRouterParams=useRouterParams,exports.useSet=useSet,exports.useStorage=useStorage,exports.useTaroId=useTaroId,exports.useThrottleFn=useThrottleFn,exports.useToggle=useToggle,exports.useTrue=useTrue,exports.useUnmount=useUnmount,exports.useUpdate=useUpdate,exports.useUpdateEffect=useUpdateEffect;