import { describe, expect, it } from 'vitest';
import {
  cleanLinkName,
  deriveLinkTagNames,
  linkDims,
  parseLinkName,
  sameOriginGroup,
  truncateToProcess,
} from './origin-name';

describe('truncateToProcess', () => {
  it('keeps first 3 dash segments (drops warehouse)', () => {
    expect(truncateToProcess('美国（包邮）180g纯棉T恤成人款-DG001-单面印花-美西洛杉矶一仓'))
      .toBe('美国（包邮）180g纯棉T恤成人款-DG001-单面印花');
  });
  it('is identity for 3-segment names', () => {
    expect(truncateToProcess('美国（不包邮）230g水洗炒雪花T恤-KRHM003-直喷双面'))
      .toBe('美国（不包邮）230g水洗炒雪花T恤-KRHM003-直喷双面');
  });
  it('handles malformed names without country prefix', () => {
    expect(truncateToProcess('（不包邮）230g水洗炒雪花T恤-FRTM001-双面印花'))
      .toBe('（不包邮）230g水洗炒雪花T恤-FRTM001-双面印花');
  });
  it('returns empty for null/empty', () => {
    expect(truncateToProcess(null)).toBe('');
    expect(truncateToProcess('')).toBe('');
  });
});

describe('sameOriginGroup', () => {
  it('matches same product across warehouses', () => {
    expect(sameOriginGroup(
      '美国（包邮）180g纯棉T恤成人款-DG001-单面印花-美西洛杉矶一仓',
      '美国（包邮）180g纯棉T恤成人款-DG001-单面印花-美中亚特兰大仓',
    )).toBe(true);
  });
  it('does not match different SKU', () => {
    expect(sameOriginGroup(
      '美国（包邮）180g纯棉T恤成人款-DG001-单面印花',
      '美国（包邮）180g纯棉T恤成人款-DG002-单面印花',
    )).toBe(false);
  });
  it('does not match different country', () => {
    expect(sameOriginGroup(
      '美国（包邮）T恤-DG001-单面印花',
      '德国（不包邮）T恤-DG001-单面印花',
    )).toBe(false);
  });
  it('does not match empty names', () => {
    expect(sameOriginGroup(null, 'x-y-z')).toBe(false);
    expect(sameOriginGroup('', '')).toBe(false);
  });
});

describe('parseLinkName / cleanLinkName', () => {
  it('parses 国家（物流）品名-SKU-工艺 → 品名 + 型号', () => {
    expect(parseLinkName('美国（包邮）180g纯棉T恤成人款-DG001-单面印花')).toEqual({
      productName: '180g纯棉T恤成人款',
      skuCode: 'DG001',
    });
    expect(cleanLinkName('美国（包邮）180g纯棉T恤成人款-DG001-单面印花')).toBe(
      '180g纯棉T恤成人款 DG001',
    );
  });

  it('handles half-width parens and warehouse suffix', () => {
    expect(
      cleanLinkName('美国(不包邮光板)180GT恤成人款-JSA002-不打印·美西洛杉矶二仓'),
    ).toBe('180GT恤成人款 JSA002');
  });

  it('returns null / raw fallback for manual names without the country(remark) structure', () => {
    expect(parseLinkName('180g纯棉T恤成人款')).toBeNull();
    expect(cleanLinkName('180g纯棉T恤成人款')).toBe('180g纯棉T恤成人款');
    expect(cleanLinkName(null)).toBe('');
  });

  it('returns null when product name after parens is empty', () => {
    expect(parseLinkName('美国（包邮）-DG001-单面印花')).toBeNull();
  });
});

describe('deriveLinkTagNames', () => {
  it('单面印花 + 包邮 + 无工艺关键字 → 单面印花/烫画/包邮', () => {
    expect(deriveLinkTagNames('美国（包邮）180g纯棉T恤成人款-DG001-单面印花')).toEqual([
      '单面印花',
      '烫画',
      '包邮',
    ]);
  });

  it('不打印 + 光板 + 不包邮 → 归并为单个不打印（光板即不打印）', () => {
    expect(
      deriveLinkTagNames('美国（不包邮光板）180GT恤成人款-JSA002-不打印·美西洛杉矶二仓'),
    ).toEqual(['不打印', '不包邮']);
  });

  it('双面印花 + 不包邮，且不误命中包邮', () => {
    expect(deriveLinkTagNames('美国（不包邮）T恤-DG001-双面印花·美东新泽西仓')).toEqual([
      '双面印花',
      '烫画',
      '不包邮',
    ]);
  });

  it('空名称返回空数组', () => {
    expect(deriveLinkTagNames(null)).toEqual([]);
  });
});

describe('linkDims', () => {
  it('解析物流备注/工艺/印花数量', () => {
    expect(linkDims('美国（包邮）180g纯棉T恤成人款-DG001-单面印花')).toEqual({
      logistics: '包邮',
      crafts: ['烫画'],
      printCount: '单面印花',
    });
    expect(linkDims('美国（不包邮光板）180GT恤成人款-JSA002-不打印·美西洛杉矶二仓')).toEqual({
      logistics: '不包邮光板',
      crafts: ['不打印'],
      printCount: null,
    });
  });

  it('无括号结构时物流为 null，手工名称工艺默认烫画', () => {
    expect(linkDims('纯棉T恤')).toEqual({ logistics: null, crafts: ['烫画'], printCount: null });
    expect(linkDims(null)).toEqual({ logistics: null, crafts: [], printCount: null });
  });
});
