import { nextTick } from 'vue';
import pinia from '../stores/inpinia.mjs';
import { storeToRefs } from 'pinia';
import { useThemeConfig } from '../stores/themeConfig.mjs';
import { i18n } from '../locale/index.mjs';

function setTagsViewNameI18n(item) {
  let tagsViewName = "";
  const { query, params, meta } = item;
  const pattern = /^\{("(zh-CN|en|zh-TW)":"[^,]+",?){1,3}}$/;
  if ((query == null ? void 0 : query.tagsViewName) || (params == null ? void 0 : params.tagsViewName)) {
    if (pattern.test(query == null ? void 0 : query.tagsViewName) || pattern.test(params == null ? void 0 : params.tagsViewName)) {
      const urlTagsParams = (query == null ? void 0 : query.tagsViewName) && JSON.parse(query == null ? void 0 : query.tagsViewName) || (params == null ? void 0 : params.tagsViewName) && JSON.parse(params == null ? void 0 : params.tagsViewName);
      tagsViewName = urlTagsParams[i18n.global.locale.value];
    } else {
      tagsViewName = (query == null ? void 0 : query.tagsViewName) || (params == null ? void 0 : params.tagsViewName);
    }
  } else {
    tagsViewName = i18n.global.t(`message.menu.${meta.title}`);
  }
  return tagsViewName;
}
const lazyImg = (el, arr) => {
  const io = new IntersectionObserver((res) => {
    res.forEach((v) => {
      if (v.isIntersecting) {
        const { img, key } = v.target.dataset;
        v.target.src = img;
        v.target.onload = () => {
          io.unobserve(v.target);
          arr[key]["loading"] = false;
        };
      }
    });
  });
  nextTick(() => {
    document.querySelectorAll(el).forEach((img) => io.observe(img));
  });
};
const globalComponentSize = () => {
  const stores = useThemeConfig(pinia);
  const { themeConfig } = storeToRefs(stores);
  return themeConfig.value.globalComponentSize;
};
function deepClone(obj) {
  let newObj;
  try {
    newObj = obj.push ? [] : {};
  } catch (error) {
    newObj = {};
  }
  for (let attr in obj) {
    if (obj[attr] && typeof obj[attr] === "object") {
      newObj[attr] = deepClone(obj[attr]);
    } else {
      newObj[attr] = obj[attr];
    }
  }
  return newObj;
}
function isMobile() {
  if (navigator.userAgent.match(/('phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone')/i)) {
    return true;
  } else {
    return false;
  }
}
function handleEmpty(list) {
  const arr = [];
  for (const i in list) {
    const d = [];
    for (const j in list[i]) {
      d.push(list[i][j]);
    }
    const leng = d.filter((item) => item === "").length;
    if (leng !== d.length) {
      arr.push(list[i]);
    }
  }
  return arr;
}
const other = {
  // elSvg: (app: App) => {
  // 	elSvg(app);
  // },
  // useTitle: () => {
  // 	useTitle();
  // },
  setTagsViewNameI18n(route) {
    return setTagsViewNameI18n(route);
  },
  lazyImg: (el, arr) => {
    lazyImg(el, arr);
  },
  globalComponentSize: () => {
    return globalComponentSize();
  },
  deepClone: (obj) => {
    return deepClone(obj);
  },
  isMobile: () => {
    return isMobile();
  },
  handleEmpty: (list) => {
    return handleEmpty(list);
  }
  // handleOpenLink: (val: RouteItem) => {
  // 	handleOpenLink(val);
  // },
};

export { deepClone, other as default, globalComponentSize, handleEmpty, isMobile, lazyImg, setTagsViewNameI18n };
