import axios from "axios";
import { store } from "../store/store";
import { SHOPTIME_BASE_URL } from "./apiConfig";

const api = axios.create({
  baseURL: SHOPTIME_BASE_URL,
});

api.defaults.headers.common["app_id"] = "com.lgshop.app";
api.defaults.headers.common["app_ver"] = "1.0.0";
api.defaults.headers.common["dvc_id"] = "testdeviceid";
api.defaults.headers.common["cntry_cd"] = "US";
api.defaults.headers.common["prod_cd"] = "webOSTV 5.0";
api.defaults.headers.common["plat_cd"] = "W20H";
api.defaults.headers.common["lang_cd"] = "en-US";
api.defaults.headers.common["os_ver"] = "3.0";
api.defaults.headers.common["sdk_ver"] = "1.0.0";
api.defaults.headers.common["publish_flag"] = "Y";

api.interceptors.request.use(
  (config) => {
    const accessToken = store.getState().auth.accessToken;

    if (accessToken) {
      config.headers["lgsp_auth"] = accessToken;
    }

    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

export default api;
