import axios from 'axios';
import {
  getTokenParams,
  getLinkParams,
  UserState,
  LoginRes,
  authParams
} from '@/api/interface';

// 修复window is not defined错误，确保只在浏览器环境中访问window对象
const defaultReqData: authParams = typeof window !== 'undefined' ? {
  domain: window.location.hostname,
  path: '/'
} : {
  domain: 'localhost',
  path: '/'
};

const urlPrefix = '/api/v1/bpms-workbench';

export function getToken(params: getTokenParams) {
  return axios.get<LoginRes>(`${urlPrefix}/sessions/callback`, {
    params: { ...params, ...defaultReqData }
  });
}

export function getAuthLink(params: getLinkParams) {
  return axios.get<string>(`${urlPrefix}/sessions/auth/url`, {
    params
  });
}

export function getUserInfo() {
  return axios.get<UserState>(`${urlPrefix}/sessions/info`);
}

export function logout() {
  return axios.get<LoginRes>(`${urlPrefix}/sessions/logout`, {
    params: defaultReqData
  });
}

export function getUserToken() {
  return axios.get<LoginRes>(`${urlPrefix}/sessions/validate`, {});
}