import { useEffect, DependencyList } from 'react';

function getWidth() {
  var width = /width=(\d*)/.exec(location.search);
  console.assert(width, '没有读取到设备宽度');
  return width ? width[1] : window.innerWidth;
}

const useScaleScreen = (
  { ref = { current: document.body }, needScale = false, baseWidth = 1920 },
  deps: DependencyList
) => {
  useEffect(() => {
    const width = parseInt(getWidth() as string, 10);
    if (ref && needScale) {
      ref.current.style.transform = `scale(${width / 1920})`;
    }
  }, deps);
};

export default useScaleScreen;
