import { UAParser } from 'ua-parser-js';
import { getLocalStorage } from '../lib';
import { StorageKeys } from './local-storage';

const { origin } = window.location;
export const CURRENT_URL = `${origin}`;

const getAPIBaseUrl = () => {
  let rs = CURRENT_URL;
  console.log('VITE_APP_ENV : ', import.meta.env.VITE_APP_ENV);
  if(import.meta.env.VITE_APP_ENV === 'production'){
    rs = import.meta.env.VITE_APP_API_PROXY_HOST;
  }
  return rs;
};
const getAPIAuthBaseUrl = () => {
  let rs = CURRENT_URL;
  console.log('VITE_APP_ENV : ', import.meta.env.VITE_APP_ENV);
  if(import.meta.env.VITE_APP_ENV === 'production'){
    rs = import.meta.env.VITE_APP_AUTH_PROXY_HOST;
  }
  return rs;
}
export const getHeaderUserAgent = (options?: {
  menuId?: number,
  apiType?: string
}) => {
  let parser = new UAParser();
  let result = parser.getResult();

  let os = result.os.name;
  let deviceType = result.device.type;
  let deviceID = getLocalStorage(StorageKeys.DeviceId);
  let appVersion = getLocalStorage(StorageKeys.AppVersion);
  let browserInformation = result.browser.name;
  
  return `${os} ${deviceType} ${deviceID} ${appVersion} ${browserInformation} ${options?.menuId || ''} ${options?.apiType || ''}`;
};

export const API_BASE_URL = getAPIBaseUrl();
export const API_AUTH_BASE_URL = getAPIAuthBaseUrl();
export const API_URL_KEY = 'nmsa';
export const API_PARAM = {};

