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: '废弃产品'
};

export function getProductStatusLabel(status: Api.Product.ProductStatusCode) {
  return productStatusRecord[status];
}

export function getProductStatusTagType(status: Api.Product.ProductStatusCode): UI.ThemeColor {
  const statusTagTypeMap: Record<Api.Product.ProductStatusCode, UI.ThemeColor> = {
    active: 'success',
    paused: 'warning',
    archived: 'info',
    abandoned: 'danger'
  };

  return statusTagTypeMap[status];
}

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';
}
