function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { useState, useEffect } from 'react';
/**
 * 设备检测Hook
 * 根据屏幕宽度和用户代理判断当前设备类型
 */
export var useDevice = function useDevice() {
  var _useState = useState(function () {
      // 初始化时的默认值
      var initialWidth = typeof window !== 'undefined' ? window.innerWidth : 1920;
      var initialHeight = typeof window !== 'undefined' ? window.innerHeight : 1080;
      var initialType = getDeviceType(initialWidth);
      return {
        type: initialType,
        isMobile: initialType === 'mobile',
        isPad: initialType === 'pad',
        isPc: initialType === 'pc',
        width: initialWidth,
        height: initialHeight
      };
    }),
    _useState2 = _slicedToArray(_useState, 2),
    deviceInfo = _useState2[0],
    setDeviceInfo = _useState2[1];
  useEffect(function () {
    var handleResize = function handleResize() {
      var width = window.innerWidth;
      var height = window.innerHeight;
      var type = getDeviceType(width);
      setDeviceInfo({
        type: type,
        isMobile: type === 'mobile',
        isPad: type === 'pad',
        isPc: type === 'pc',
        width: width,
        height: height
      });
    };

    // 初次加载时检测一次
    handleResize();

    // 监听窗口尺寸变化
    window.addEventListener('resize', handleResize);

    // 清理监听器
    return function () {
      window.removeEventListener('resize', handleResize);
    };
  }, []);
  return deviceInfo;
};

/**
 * 根据屏幕宽度判断设备类型
 * @param width 屏幕宽度
 * @returns 设备类型
 */
function getDeviceType(width) {
  // 移动端：小于 768px
  if (width < 768) {
    return 'mobile';
  }
  // 平板：768px - 1200px
  else if (width < 1200) {
    return 'pad';
  }
  // PC端：大于等于 1200px
  else {
    return 'pc';
  }
}

/**
 * 获取用户代理信息
 * @returns 用户代理相关信息
 */
export var getUserAgentInfo = function getUserAgentInfo() {
  if (typeof window === 'undefined' || !window.navigator) {
    return {
      isMobileUA: false,
      isTabletUA: false,
      isDesktopUA: true,
      userAgent: ''
    };
  }
  var userAgent = window.navigator.userAgent.toLowerCase();
  var isMobileUA = /mobile|android|iphone|ipod|phone/.test(userAgent);
  var isTabletUA = /tablet|ipad/.test(userAgent) && !isMobileUA;
  var isDesktopUA = !isMobileUA && !isTabletUA;
  return {
    isMobileUA: isMobileUA,
    isTabletUA: isTabletUA,
    isDesktopUA: isDesktopUA,
    userAgent: userAgent
  };
};