export const setLocalStorage = (key: string, value: any) => {
  if (!key) return false;
  if (value) {
    if (typeof value === 'object') {
      window?.localStorage?.setItem(key, JSON.stringify(value));
    } else {
      window?.localStorage?.setItem(key, value);
    }
  }
  return true;
};

export const getLocalStorage = (key: string) => {
  if (!key) return;
  const ret = window?.localStorage?.getItem(key);

  try {
    return ret && JSON.parse(ret);
  } catch (error) {
    return ret;
  }
};

export const parseValueToDate = (
  date: string | number | Date | string[] | number[] | null | undefined,
  type?: string,
  fixType?: string,
): string => {
  if (!date) return '';
  let parse = '';
  switch (typeof date) {
    case 'string':
      return date;
    case 'number':
    case 'object':
      let dDate = null;
      if (date instanceof Date || typeof date === 'number') {
        dDate = new Date(date);
      } else if (date instanceof Array) {
        if (date.length === 6)
          dDate = new Date(
            Number(date[0]),
            Number(date[1]) - 1,
            Number(date[2]),
            Number(date[3]),
            Number(date[4]),
            Number(date[5]),
          );
        else if (date.length === 5)
          dDate = dDate = new Date(
            Number(date[0]),
            Number(date[1]) - 1,
            Number(date[2]),
            Number(date[3]),
            Number(date[4]),
          );
        else if (date.length === 4)
          dDate = dDate = new Date(Number(date[0]), Number(date[1]) - 1, Number(date[2]), Number(date[3]));
        else if (date.length === 3) dDate = dDate = new Date(Number(date[0]), Number(date[1]) - 1, Number(date[2]));
        else if (date.length === 2) dDate = dDate = new Date(Number(date[0]), Number(date[1]) - 1);
        else if (date.length === 1) dDate = new Date(Number(date[0]));
      }

      if (dDate) {
        if (type !== 'time') {
          parse = dDate.getFullYear() + (fixType ? fixType : '-');
          parse += String(dDate.getMonth() + 1).padStart(2, '0') + (fixType ? fixType : '-');
          parse += String(dDate.getDate()).padStart(2, '0');
        }
        if (type !== 'date') {
          if (type !== 'time') parse += ' ';
          parse += String(dDate.getHours()).padStart(2, '0') + ':';
          parse += String(dDate.getMinutes()).padStart(2, '0') + ':';
          parse += String(dDate.getSeconds()).padStart(2, '0');
        }
      }
      return parse;
  }
};
