// 平台类方法，如 BOM、DOM、区分 h5 和 pc 端
import { useMedia } from 'react-use';
/**
 * 当前环境是否为 H5 端
 */
export const isH5Platform = () => {
  if (window.matchMedia) {
    return window.matchMedia('(max-width: 1024px)')?.matches;
  } else {
    let is = false;
    try {
      const ua = navigator && navigator.userAgent;
      is = /Android|webOS|BlackBerry|iPhone|iPad|iPod|iOS/i.test(ua);
    } catch (e) {
      null;
    }
    return is;
  }
};

/**
 * 判断当前是否是 cloud 开头的图片协议
 */
export const isCloudFileID = (url) => {
  return /^cloud:\/\/.*/.test(url);
};

export function usePlatform() {
  const smallScreen = useMedia('(max-width: 1024px)', isH5Platform());
  const touchDevice = useMedia('(pointer:coarse)', smallScreen);
  return (touchDevice || smallScreen) ? 'h5' : 'pc';
}