import { transformRecordToOption } from '@/utils/common';

export const productStatusRecord: Record<Api.Product.ProductStatusCode, string> = {
  active: '启用',
  paused: '暂停',
  archived: '归档',
  abandoned: '废弃'
};

export const productStatusOptions = transformRecordToOption(productStatusRecord);

export const productStatusActionRecord: Record<Api.Product.ProductStatusActionCode, string> = {
  pause: '暂停产品',
  resume: '恢复产品',
  archive: '归档产品',
  abandon: '废弃产品'
};

/** 状态编码来自状态机动态下发，未配置的新状态回退编码本身（展示名优先用后端 statusName） */
export function getProductStatusLabel(status: string) {
  return (productStatusRecord as Record<string, string>)[status] ?? status;
}

/** 根据产品状态返回对应的 Tag 类型；未配置的新状态回退 info */
export function getProductStatusTagType(status: string): UI.ThemeColor {
  const statusTagTypeMap: Record<string, UI.ThemeColor> = {
    active: 'success',
    paused: 'warning',
    archived: 'info',
    abandoned: 'danger'
  };

  return statusTagTypeMap[status] ?? 'info';
}

export function isProductEditable(status: Api.Product.ProductStatusCode) {
  return status === 'active' || status === 'paused';
}

export function isProductEditLimited(status: Api.Product.ProductStatusCode) {
  return status === 'paused';
}

export function getAllowedProductStatusActions(
  status: Api.Product.ProductStatusCode
): Api.Product.ProductStatusActionCode[] {
  const actionMap: Record<Api.Product.ProductStatusCode, Api.Product.ProductStatusActionCode[]> = {
    active: ['pause', 'archive', 'abandon'],
    paused: ['resume', 'archive', 'abandon'],
    archived: [],
    abandoned: []
  };

  return actionMap[status];
}

export function getProductStatusActionLabel(actionCode: Api.Product.ProductStatusActionCode) {
  return productStatusActionRecord[actionCode];
}

export function getProductStatusActionOptions(status: Api.Product.ProductStatusCode) {
  return getAllowedProductStatusActions(status).map(actionCode => ({
    value: actionCode,
    label: getProductStatusActionLabel(actionCode)
  }));
}

export function isProductActionReasonRequired(actionCode: Api.Product.ProductStatusActionCode) {
  return actionCode !== 'resume';
}
