import logger from "@racletteCore/lib/logger"
import { PLUGIN_ERROR_FLAGS } from "../constants"
import type { PluginType } from "../types"

/**
 * Handle plugin errors based on error flags
 */
export const handlePluginError = (
  error: string,
  pluginType: PluginType,
  errorFlags: number,
): void => {
  const shouldCrash =
    (errorFlags &
      PLUGIN_ERROR_FLAGS[
        pluginType.toUpperCase() as keyof typeof PLUGIN_ERROR_FLAGS
      ]) !==
    0

  if (shouldCrash) {
    logger.pluginRegistrar(error)
    throw new Error(error)
  } else {
    logger.pluginRegistrar(error)
  }
}
