import type { TemplateHealthAction, TemplateStatus } from '#/api/x/eln/templates';

export type TemplateAction = 'annotate' | 'createVersion' | 'disable' | 'enable' | 'health' | 'publish' | 'setCurrent';
export type HealthAction = 'confirmRemove' | 'locate' | 'repair';

const serverHealthActions: Record<HealthAction, TemplateHealthAction> = {
  confirmRemove: 'CONFIRM_REMOVE',
  locate: 'LOCATE',
  repair: 'REPAIR',
};

export function templateActions(input: { current: boolean; enabled: boolean; status: TemplateStatus }): TemplateAction[] {
  const actions: TemplateAction[] = [];
  if (input.status === 'DRAFT') actions.push('annotate');
  actions.push('health');
  if (input.status === 'DRAFT') actions.push('publish');
  actions.push('createVersion');
  if (input.status === 'PUBLISHED' && input.enabled && !input.current) actions.push('setCurrent');
  actions.push(input.enabled ? 'disable' : 'enable');
  return actions;
}

export function healthActions(input: { actions?: TemplateHealthAction[]; bindingId?: string; code: string }): HealthAction[] {
  const actions = new Set(input.actions);
  return [
    ...(actions.has(serverHealthActions.locate) ? ['locate'] as const : []),
    ...(input.bindingId && actions.has(serverHealthActions.repair) ? ['repair'] as const : []),
    ...(input.bindingId && actions.has(serverHealthActions.confirmRemove) ? ['confirmRemove'] as const : []),
  ];
}
