export interface ApiRequestItem {
  url: string;
  method: string;
  startTime: string;
  uuid: string;
}

const apiRequestKey = 'apiRequestStorage'

export const getRequestData = () => {
  return JSON.parse(localStorage.getItem(apiRequestKey) || '[]')
}
export const getTimeDifferenceInMilliseconds = (time1:string, time2:string): number | null => {
  const date1 = new Date(time1);
  const date2 = new Date(time2);
  // 检查日期是否有效
  if (isNaN(date1.getTime()) || isNaN(date2.getTime())) {
    return null; // 返回 null 或者其他你想要的错误处理
  }
  return date2.getTime() - date1.getTime();
}

export const findRequestItemByUUID = (uuid: string) => {
  const data = getRequestData()
  return data.find((d: ApiRequestItem) => d.uuid === uuid)
}

export const deleteRequestByUUID = (uuid: string) => {
  let data = getRequestData()
  data= data.filter((d: ApiRequestItem) => d.uuid !== uuid)
  localStorage.setItem(apiRequestKey,JSON.stringify(data))
}

export const addRequestItem = (item?:ApiRequestItem) => {
  const data = getRequestData()
  data.push(item)
  localStorage.setItem(apiRequestKey,JSON.stringify(data))
  return  data
}
