/**
 * 组装特色方提交 payload，编辑药方时需原样带回 meta 字段，避免清空介绍图等
 */
export function buildSpecialPrescriptionPayload(
  detail: Record<string, any>,
  drugOverrides?: {
    drugs?: any[];
    dosage?: number;
    day_dosage?: number;
    rule_type?: number;
    package_method_id?: number;
    clinical_diagnose?: string;
    doctor_order?: string;
  },
) {
  const payload: Record<string, any> = {
    id: detail.id,
    name: detail.name || '',
    category_id: detail.category_id,
    cover_image: detail.cover_image || '',
    tags: Array.isArray(detail.tags) ? detail.tags : [],
    price_per_dose: detail.price_per_dose ?? 0,
    sales_count: detail.sales_count ?? 0,
    status: detail.status ?? 1,
    is_free_shipping: detail.is_free_shipping ?? 0,
    prescription_type: detail.prescription_type || 'chinese',
    intro_text: detail.intro_text || '',
    introduction_images: detail.introduction_images || [],
    clinical_diagnose: detail.clinical_diagnose || '',
    doctor_order: detail.doctor_order || '',
  };
  if (drugOverrides?.drugs?.length) {
    payload.drugs = drugOverrides.drugs;
    payload.dosage = drugOverrides.dosage ?? 7;
    payload.day_dosage = drugOverrides.day_dosage ?? 2;
    payload.rule_type = drugOverrides.rule_type ?? 1;
    payload.package_method_id = drugOverrides.package_method_id ?? 2;
  }
  if (drugOverrides?.clinical_diagnose !== undefined) {
    payload.clinical_diagnose = drugOverrides.clinical_diagnose;
  }
  if (drugOverrides?.doctor_order !== undefined) {
    payload.doctor_order = drugOverrides.doctor_order;
  }
  return payload;
}
