export const storage = {
  get: (key) => {
    return localStorage.getItem(key);
  },
  set: (key, value) => {
    localStorage.setItem(key, value);
  },
  getToken: () => {
    const authString = localStorage.getItem("autoflow-auth");
    if (authString !== null) {
      try {
        const auth = JSON.parse(authString);

        if (auth.jwtCookie) {
          const match = auth.jwtCookie.match(/cuuva-jwt=([^;]+)/);
          if (match && match[1]) {
            return match[1];
          }
        }
      } catch (e) {
        console.error("[storage] getToken parse error:", e);
      }
    }
    return "";
  },

  // 리프레시 토큰만 잘라서 반환
  getRefreshToken: () => {
    const authString = localStorage.getItem("autoflow-auth");
    if (authString !== null) {
      try {
        const auth = JSON.parse(authString);

        if (auth.jwtRefreshCookie) {
          const match = auth.jwtRefreshCookie.match(
            /cuuva-jwt-refresh=([^;]+)/,
          );
          if (match && match[1]) {
            return match[1];
          }
        }
      } catch (e) {
        console.error("[storage] getRefreshToken parse error:", e);
      }
    }
    return "";
  },
  getUserId: () => {
    const authString = localStorage.getItem("autoflow-auth");
    if (authString !== null) {
      const auth = JSON.parse(authString);
      return auth.userId;
    }
    return "";
  },
  getId: () => {
    const authString = localStorage.getItem("autoflow-auth");
    if (authString !== null) {
      const auth = JSON.parse(authString);
      return auth.id;
    }
    return "";
  },

  getAuth: () => {
    const authString = localStorage.getItem("autoflow-auth");
    if (authString !== null) {
      return JSON.parse(authString);
    }
    return null;
  },
  setAuth: (value) => {
    localStorage.setItem("autoflow-auth", JSON.stringify(value));
  },
  clearAuth: () => {
    localStorage.removeItem("autoflow-auth");
  },
};
