/**
 * 文件职责：分类管理常量与基础转换工具。
 * 1. 统一渠道字段标准化逻辑。
 * 2. 避免页面编排层重复处理映射细节。
 */
import type {
  ProductCategoryChannel,
  ProductCategoryManageDto,
} from '#/api/product';

import { CATEGORY_CHANNEL_ORDER } from '../../types';

export function normalizeCategoryChannels(
  channels: ProductCategoryManageDto['channels'],
) {
  const source = Array.isArray(channels)
    ? (channels as Array<'ts' | 'zt' | ProductCategoryChannel>)
    : [];

  const normalized = source
    .map((channel) => {
      if (channel === 'zt') return 'pickup';
      if (channel === 'ts') return 'dine_in';
      return channel;
    })
    .filter((channel): channel is ProductCategoryChannel =>
      CATEGORY_CHANNEL_ORDER.includes(channel as ProductCategoryChannel),
    );

  const unique = [...new Set(normalized)];
  if (unique.length > 0) {
    return unique;
  }

  return [];
}
