// src/utils/site.ts

// 开发环境：端口 → 站点
const DEV_PORT_MAP: Record<string, any> = {
  '5101': 'www',
  '5102': 'b',
  '5103': 'mro',
  '5104': 'fuli',
  '5105': 'reg',
  '5106': 'breg',
  '5107': 'greg',
  '5108': 'passport',
  '5109': 'search',
  '5110': 'item',
  '5111': 'cart',
  '5112': 'trad',
  '5113': 'payc',
  '5114': 'order',
  '5115': 'plan',
  '5116': 'plan_info',
  '5117': 'i',
  '5118': 'easybuv'
};

// 每个站点允许的路由
export const SITE_ROUTES: Record<any, string[]> = {
  www: ['/', '/index'], //优易365主站
  b: ['/indexB'], //企业购商城
  mro: ['/indexMro'], //工业品商城
  fuli: ['/indexFuli'], //福礼商城
  reg: ['/reg'], //个人注册
  breg: ['/breg'], //企业注册
  greg: ['/greg'], //供应商注册
  passport: ['/login'], //登录页
  search: ['/search', '/search/special'], //搜索
  item: ['/item'], //商品详情,
  cart: ['/cart'], //商品详情
  trad: ['/trad'], //确认订单信息
  payc: ['/payc'], //支付订单

  plan: ['/plan', '/plan/procure', '/plan/guide', '/plan/project'], //解决方案
  plan_info: ['/plan_info', '/plan_info/procure', '/plan_info/guide', '/plan_info/project'], //信息展示

  order: [
    '/order/orderManage',
    '/order/orderManage/detail',
    '/order/orderAudit',
    '/order/afterSale',
    '/order/batchOrder',
    '/order/orderEvaluation',
    '/enterprise/companyInfo',
    '/enterprise/companyInfo/edit',
    '/enterprise/purchaseHabit',
    '/enterprise/invoiceManage',
    '/enterprise/agreementSupply',
    '/enterprise/companyInfo',
    '/enterprise/invoiceManage',
    '/enterprise/myCollection',
    '/enterprise/myFootprint',
    '/enterprise/purchaseHabit',
    '/enterprise/purchasePlan',
    '/enterprise/purchaseHistory',
    '/reconciliation/billManage',
    '/reconciliation/invoiceManage',
    '/organization/deptManage',
    '/organization/staffManage',
    '/organization/roleManage',
    '/valueAdded/maintenance',
    '/valueAdded/complaint',
    '/cost/itemExpense',
    '/cost/quotaControl',
    '/cost/quotaControl/apply',
    '/enterprise/purchasePlan',
    '/organization/approvalFlow',
    '/organization/approvalFlow/create',
    '/order/orderManage/detail/:orderNo',
    '/order/orderManage/applyAfter',
    '/valueAdded/maintenanceApply'
  ], //订单列表

  i: ['/i'], //个人信息
  easybuv: ['/easybuv'] //地址管理
};

// 获取当前站点（开发用端口，线上用域名）
export function getCurrentSite(): any {
  if (import.meta.env.PROD) {
    // 线上：根据域名判断
    const host = window.location.hostname;
    if (host === 'b.yoe365.com') return 'b';
    if (host === 'mro.yoe365.com') return 'mro';
    if (host === 'mro.yoe365.com') return 'fuli';
    if (host === 'mro.yoe365.com') return 'reg';
    if (host === 'mro.yoe365.com') return 'breg';
    if (host === 'mro.yoe365.com') return 'greg';
    if (host === 'mro.yoe365.com') return 'passport';
    if (host === 'mro.yoe365.com') return 'search';
    if (host === 'mro.yoe365.com') return 'item';
    if (host === 'mro.yoe365.com') return 'cart';
    if (host === 'mro.yoe365.com') return 'trad';
    if (host === 'mro.yoe365.com') return 'payc';
    if (host === 'mro.yoe365.com') return 'order';
    if (host === 'mro.yoe365.com') return 'plan';
    if (host === 'mro.yoe365.com') return 'plan_info';
    if (host === 'mro.yoe365.com') return 'i';
    if (host === 'mro.yoe365.com') return 'easybuv';
    return 'www';
  }

  // 本地：根据端口判断
  const port = window.location.port || '80';
  return DEV_PORT_MAP[port] || 'www';
}

// 根据站点返回 API 基地址

export function getApiBase() {
  const site = getCurrentSite();
  const map: Record<any, string> = {
    www: 'https://www.yoe365.com',
    b: 'https://b.yoe365.com',
    mro: 'https://mro.yoe365.com',
    fuli: 'https://fuli.yoe365.com',
    reg: 'https://reg.yoe365.com',
    breg: 'https://breg.yoe365.com',
    greg: 'https://greg.yoe365.com',
    passport: 'https://passport.yoe365.com',
    search: 'https://search.yoe365.com',
    item: 'https://item.yoe365.com',
    cart: 'https://cart.yoe365.com',
    trad: 'https://trad.yoe365.com',
    payc: 'https://payc.yoe365.com',
    order: 'https://order.yoe365.com',
    plan: 'https://plan.yoe365.com',
    plan_info: 'https://plan_info.yoe365.com',
    i: 'https://i.yoe365.com',
    easybuv: 'https://easybuv.yoe365.com'
  };
  if (import.meta.env.PROD) {
    return map[site];
  } else {
    // return '/dev-api'
    // return 'http://192.168.1.52:8080';
    // return 'https://ceshi.xiaoluwebsite.xyz';
    return 'http://localhost:8080';
  }
}

const PATH_TO_SITE_MAP: Record<string, any> = {};
for (const [site, paths] of Object.entries(SITE_ROUTES)) {
  for (const path of paths) {
    PATH_TO_SITE_MAP[path] = site as any;
  }
}

export function getSiteByPath(path: string): any | null {
  // 支持带查询参数（如 /login?redirect=xxx）
  const cleanPath = path.split('?')[0];
  return PATH_TO_SITE_MAP[cleanPath] || null;
}
export function onPath(path: string) {
  const targetSite = getSiteByPath(path);
  if (!targetSite) {
    // window.open(url, '_blank');
    console.error(`[跨站跳转失败] 路径 "${path}" 未关联任何站点`);
    return;
  }

  let url = '';

  if (import.meta.env.PROD) {
    const domainMap: Record<any, string> = {
      www: 'https://www.yoe365.com',
      b: 'https://b.yoe365.com',
      mro: 'https://mro.yoe365.com',
      fuli: 'https://fuli.yoe365.com',
      reg: 'https://reg.yoe365.com',
      breg: 'https://breg.yoe365.com',
      greg: 'https://greg.yoe365.com',
      passport: 'https://passport.yoe365.com',
      search: 'https://search.yoe365.com',
      item: 'https://item.yoe365.com',
      cart: 'https://cart.yoe365.com',
      trad: 'https://trad.yoe365.com',
      payc: 'https://payc.yoe365.com',
      order: 'https://order.yoe365.com',
      plan: 'https://plan.yoe365.com',
      plan_info: 'https://plan_info.yoe365.com',
      i: 'https://i.yoe365.com',
      easybuv: 'https://easybuv.yoe365.com'
    };
    url = `${domainMap[targetSite]}${path}`;
  } else {
    const portMap: Record<any, string> = {
      www: 'http://localhost:5101',
      b: 'http://localhost:5102',
      mro: 'http://localhost:5103',
      fuli: 'http://localhost:5104',
      reg: 'http://localhost:5105',
      breg: 'http://localhost:5106',
      greg: 'http://localhost:5107',
      passport: 'http://localhost:5108',
      search: 'http://localhost:5109',
      item: 'http://localhost:5110',
      cart: 'http://localhost:5111',
      trad: 'http://localhost:5112',
      payc: 'http://localhost:5113',
      order: 'http://localhost:5114',
      plan: 'http://localhost:5115',
      plan_info: 'http://localhost:5116',
      i: 'http://localhost:5117',
      easybuv: 'http://localhost:5118'
    };
    url = `${portMap[targetSite]}${path}`;
  }

  window.open(url, '_blank');
}
