export type ComputerErrorCode =
  | 'UNSUPPORTED_PLATFORM'
  | 'BACKEND_UNAVAILABLE'
  | 'DISPLAY_NOT_FOUND'
  | 'OUT_OF_BOUNDS'
  | 'INVALID_INPUT'
  | 'DEPENDENCY_MISSING'
  | 'ACTION_DISABLED'
  | 'OBSERVE_DISABLED'
  | 'OUTPUT_LIMIT'
  | 'OS_ERROR';

export interface ComputerError extends Error {
  readonly code: ComputerErrorCode;
  readonly operation: string;
  readonly details?: Readonly<Record<string, unknown>>;
}

export function computerError(
  code: ComputerErrorCode,
  operation: string,
  message: string,
  details?: Readonly<Record<string, unknown>>,
): ComputerError {
  const error = new Error(message) as ComputerError;
  Object.defineProperties(error, {
    code: { value: code, enumerable: true },
    operation: { value: operation, enumerable: true },
    ...(details === undefined ? {} : { details: { value: details, enumerable: true } }),
  });
  return error;
}

export function isComputerError(value: unknown): value is ComputerError {
  if (!(value instanceof Error)) return false;
  const candidate = value as Partial<ComputerError>;
  return typeof candidate.code === 'string' && typeof candidate.operation === 'string';
}
