import type { ComponentRef } from 'react';
import ImageFromView from './NativeImageFromView';
import { findNodeHandle } from 'react-native';

class MakeImageError extends Error {
  constructor(message: string) {
    super(message);
  }
}

export enum OutputType {
  BASE64 = 'base64',
}

export function makeImageFromView(
  containerRef: ComponentRef<any>
): Promise<string> {
  const tag = findNodeHandle(containerRef as any);

  if (!tag) throw new MakeImageError('Container not found');

  return ImageFromView.makeImageFromView(tag, {
    outputType: OutputType.BASE64,
  });
}
