import type {
  ComponentPlugin,
  ComponentPluginFactory,
  PropsPlugin,
} from '../../../types/component-plugin';
import type { StatusBadgePluginInput, StatusBadgeProps, StatusTone } from '../types';

export type StatusBadgePluginOptions = {
  fallbackTone?: StatusTone;
};

const statusToneMap: Record<NonNullable<StatusBadgePluginInput['status']>, StatusTone> = {
  ready: 'default',
  working: 'processing',
  blocked: 'error',
  done: 'success',
};

export const mapStatusBadgeInputToProps: ComponentPlugin<
  StatusBadgePluginInput,
  StatusBadgeProps
> = (input) => ({
  label: input.text,
  tone: input.status ? statusToneMap[input.status] : 'default',
});

export const createStatusBadgeTonePlugin: ComponentPluginFactory<
  [options?: StatusBadgePluginOptions],
  StatusBadgeProps,
  StatusBadgeProps
> =
  (options) =>
  (props) => ({
    ...props,
    tone: props.tone === 'default' ? options?.fallbackTone ?? 'default' : props.tone,
  });

export const emphasizeDonePlugin =
  (input: StatusBadgePluginInput): PropsPlugin<StatusBadgeProps> =>
  (props) => ({
    ...props,
    label: input.status === 'done' ? `${props.label} 완료` : props.label,
  });
