/**
 * 适配器注册中心
 * 所有平台适配器在此注册，自动发现和加载
 */

const CrmeBAdapter = require('./crmeB');
const ShopXOAdapter = require('./shopxo');
const LikeShopAdapter = require('./likeshop');
const TaobaoAdapter = require('./taobao');
const PinduoduoAdapter = require('./pinduoduo');
const CrmeBXlsxAdapter = require('./crmeB-xlsx');

// 注册所有适配器
const REGISTRY = [
  new CrmeBAdapter(),
  new ShopXOAdapter(),
  new LikeShopAdapter(),
  new TaobaoAdapter(),
  new PinduoduoAdapter(),
  new CrmeBXlsxAdapter(),
];

/**
 * 获取所有已注册的适配器
 */
function getAllAdapters() {
  return REGISTRY.map(a => ({
    name: a.name,
    label: a.label,
    version: a.version,
    description: a.description,
    icon: a.icon,
    formats: a.formats,
    platformVersion: a.platformVersion,
  }));
}

/**
 * 根据名称查找适配器
 */
function getAdapter(name) {
  return REGISTRY.find(a => a.name === name);
}

/**
 * 自动检测输入数据属于哪个平台
 * @param {Object} input - { type: 'sql'|'json', content: string }
 * @returns {Object|null} { adapter, score }
 */
function detectAdapter(input) {
  let best = { adapter: null, score: 0 };
  
  for (const adapter of REGISTRY) {
    const score = adapter.detect(input);
    if (score > best.score) {
      best = { adapter, score };
    }
  }
  
  return best.adapter ? best : null;
}

/**
 * 获取所有适配器的帮助文档
 */
function getAllHelp() {
  return REGISTRY.map(a => ({
    name: a.name,
    label: a.label,
    icon: a.icon,
    help: a.getHelp(),
  }));
}

module.exports = {
  getAllAdapters,
  getAdapter,
  detectAdapter,
  getAllHelp,
  REGISTRY,
};
