/**
 * Used to monitor routing changes to change the status of menus and tabs. There is no need to monitor the route, because the route status change is affected by the page rendering time, which will be slow
 *
 * @format
 */

import mitt from './mitt';

const emitter = mitt();
const key = Symbol();
let appInfo;
export function getAppInfo(appInfo) {
  if (!appInfo) return appInfo;
  return appInfo;
  //   const { matched, ...opt } = appInfo;
  //   return {
  //     ...opt,
  //     matched: (matched
  //       ? matched.map((item) => ({
  //           meta: item.meta,
  //           name: item.name,
  //           path: item.path,
  //         }))
  //       : undefined) as RouteRecordNormalized[],
  //   };
}

export function setAppClickChange(info) {
  const r = getAppInfo(info);
  emitter.emit(key, r);
  appInfo = r;
}
export function listenerAppClickChange(callback: (val) => void) {
  emitter.on(key, callback);
  //  appInfo && callback(appInfo);
}

export function removeAppClickChangeListener() {
  emitter.clear();
}

//防抖函数
type CallbackFn = (item?: any) => void;

let timer: any = null;
export function useDebounce(Callback: CallbackFn, delay = 500) {
  timer != null ? clearTimeout(timer) : null;
  timer = setTimeout(() => {
    Callback && Callback(); //当有值才会执行
  }, delay);
}
