/**
 * 是否为手机端
 */
export const isMobile = () => {
  const userAgent = window.navigator.userAgent;
  if (userAgent) {
    if (
      /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
        userAgent
      )
    ) {
      // 是手机端
      return true;
    }
    return false;
  }
};

/**
 * 函数节流处理
 */
export function throttle(func: () => void, delay: number) {
  let timer: any = null;
  let startTime = Date.now();
  return function (this: any, ...args: any[]) {
    const curTime = Date.now();
    const remaining = delay - (curTime - startTime);
    const context = this;
    clearTimeout(timer);
    if (remaining <= 0) {
      func.call(context, ...args);
      startTime = Date.now();
    } else {
      timer = setTimeout(() => {
        func.call(context, ...args);
      }, remaining);
    }
  };
}

/**
 * 防抖--可选择首次是否执行
 * @param func -fn
 * @param delay -time
 * @param Immediate -首次立即执行
 * @returns
 */
export function debounce(func: () => void, delay: number, Immediate = false) {
  let timer: any = null;
  let closeImmed: any = null;
  let immed = Immediate;
  return function (this: any, ...args: any) {
    const context = this;
    if (Immediate) {
      if (closeImmed) {
        clearTimeout(closeImmed);
      }
      if (immed) {
        func.call(context, ...args);
        immed = false;
        closeImmed = setTimeout(() => {
          immed = true;
        }, delay);
        return;
      }
    }
    if (timer) {
      clearTimeout(timer);
    }
    timer = setTimeout(() => {
      func.call(context, ...args);
      immed = true;
    }, delay);
  };
}

// 表格排序--降序
export const TableSort = (list: any, field: string) => {
  let minIndex, temp;
  for (let i = 0; i < list.length - 1; i++) {
    minIndex = i;
    for (let j = i + 1; j < list.length; j++) {
      if (list[j][field] > list[minIndex][field]) {
        minIndex = j;
      }
    }
    temp = list[i];
    list[i] = list[minIndex];
    list[minIndex] = temp;
  }
  return list;
};
/**
 * 合并去重
 * @param list :数据源
 * @param newList :新进来的数据
 */
export const filterRepeatTimestamp = (list: any, newList: any) => {
  for (let i = 0; i < newList.length; i++) {
    const index = list.findIndex((item: any) => item.id == newList[i].id);
    if (index == -1) {
      // 没找到重复的
      list.push(newList[i]);
    }
  }
  return list;
};

/**
 * 后台配置组件正则校验
 * @param rules
 * @param value
 * @returns
 */
export const Validationrules = (
  rules: any[],
  value: string | number,
  name: string = ''
) => {
  if (rules && rules.length) {
    for (let i = 0; i < rules.length; i++) {
      const item = rules[i];
      if (item.type == 'required' && !value && name.indexOf('size_') == -1) {
        //  必填项
        return item.message;
      } else if (item.type == 'regex' && value) {
        // 正则
        const status = item.value.test(value);
        if (!status) {
          return item.message;
        }
      }
    }
  } else {
    return '';
  }
};

export const ChangePageHeight = (el: HTMLElement) => {
  if (el) {
    // 获取元素高度
    const el_height = el.clientHeight;
    const client = el.getBoundingClientRect();
    if (client.bottom > el_height) {
      // 铺满屏幕
      el.style.height = '100%';
      el.style.minHeight = el_height + 'px';
    }
  }
};

/**
 * 获取本地数据
 * @param key
 * @param type
 * @returns
 */
export const getLocalData = (
  key: string,
  type: 'local' | 'session' = 'local'
) => {
  if (type == 'local') {
    const data = localStorage.getItem(key);
    if (data) {
      return JSON.parse(data);
    }
    return '';
  } else if (type == 'session') {
    const data = sessionStorage.getItem(key);
    if (data) {
      return JSON.parse(data);
    }
    return '';
  }
};

/**
 * 修改本地数据
 * @param key
 * @param type
 */
export const setLocalData = (
  key: string,
  params: any,
  type: 'local' | 'session' = 'local'
) => {
  if (type == 'local') {
    localStorage.setItem(key, JSON.stringify(params));
  } else if (type == 'session') {
    sessionStorage.setItem(key, JSON.stringify(params));
  }
};

/**
 * 获取可视区域大小
 * @returns
 */
export const getDocumentSize = () => {
  if (window.innerHeight !== undefined) {
    return {
      width: window.innerWidth,
      height: window.innerHeight,
    };
  } else if (document.compatMode === 'CSS1Compat') {
    return {
      width: document.documentElement.clientWidth,
      height: document.documentElement.clientHeight,
    };
  } else {
    return {
      width: document.body.clientWidth,
      height: document.body.clientHeight,
    };
  }
};
