import type { PreviewPipelinePhaseStatus, PreviewSourceKind } from './previewSessionState';

export type PreviewPipelineStatusSnapshot = {
  shapeStatus: PreviewPipelinePhaseStatus;
  renderStatus: PreviewPipelinePhaseStatus;
  verifyStatus: PreviewPipelinePhaseStatus;
};

export const hasValidPreviewSelectionForSource = (params: {
  sourceKind: PreviewSourceKind;
  selectedInvoiceId: string | null;
  selectedInvoiceData: unknown;
  previewData: unknown;
}): boolean => {
  if (params.sourceKind === 'sample') {
    return Boolean(params.previewData);
  }
  return Boolean(params.selectedInvoiceId && params.selectedInvoiceData);
};

export const hasRenderablePreviewOutput = (params: {
  previewData: unknown;
  renderStatus: 'idle' | 'success' | 'error';
  html: string | null;
}): boolean =>
  Boolean(params.previewData && params.renderStatus === 'success' && params.html && params.html.trim().length > 0);

export const derivePreviewPipelineDisplayStatuses = (params: {
  statuses: PreviewPipelineStatusSnapshot;
  canDisplaySuccessStates: boolean;
}): PreviewPipelineStatusSnapshot => {
  const normalize = (status: PreviewPipelinePhaseStatus): PreviewPipelinePhaseStatus =>
    status === 'success' && !params.canDisplaySuccessStates ? 'idle' : status;

  return {
    shapeStatus: normalize(params.statuses.shapeStatus),
    renderStatus: normalize(params.statuses.renderStatus),
    verifyStatus: normalize(params.statuses.verifyStatus),
  };
};
