import type { PluginListenerHandle } from '@capacitor/core';

const CAPACITOR_CORE_MODULE_ID = '@capacitor/core';

type CapacitorCoreModule = typeof import('@capacitor/core');

const loadCapacitorCore = async (): Promise<CapacitorCoreModule> =>
  import(/* @vite-ignore */ CAPACITOR_CORE_MODULE_ID);

/** Metadata for a file received from an Android share intent. */
export type AndroidSharedFile = {
  path: string;
  name: string;
  mimeType: string;
  size: number;
};

/** Payload persisted by the native Android share handler. */
export type AndroidSharePayload = {
  text?: string;
  subject?: string;
  files: AndroidSharedFile[];
  receivedAt: number;
};

interface AndroidShareHandlerPlugin {
  /** Returns the last pending share payload, if one exists. */
  getPendingShare(): Promise<{ share: AndroidSharePayload | null }>;
  /** Clears the last pending share payload after it has been consumed. */
  clearPendingShare(): Promise<void>;
  addListener(
    eventName: 'shareReceived',
    listenerFunc: (payload: AndroidSharePayload) => void
  ): Promise<PluginListenerHandle>;
}

const getAndroidShareHandler = async (): Promise<AndroidShareHandlerPlugin> => {
  const { registerPlugin } = await loadCapacitorCore();
  return registerPlugin<AndroidShareHandlerPlugin>('AndroidShareHandler');
};

/** Returns true when the Android share bridge is available. */
export const isAndroidShareSupported = (): boolean => {
  const capacitor = (window as typeof window & { Capacitor?: CapacitorCoreModule['Capacitor'] }).Capacitor;
  return Boolean(capacitor?.isNativePlatform?.() && capacitor.getPlatform() === 'android');
};

/** Fetches the current pending native share payload. */
export const getPendingAndroidShare = async (): Promise<AndroidSharePayload | null> => {
  if (!isAndroidShareSupported()) return null;
  const AndroidShareHandler = await getAndroidShareHandler();
  const result = await AndroidShareHandler.getPendingShare();
  return result.share;
};

/** Clears the native pending share payload. */
export const clearPendingAndroidShare = async (): Promise<void> => {
  if (!isAndroidShareSupported()) return;
  const AndroidShareHandler = await getAndroidShareHandler();
  await AndroidShareHandler.clearPendingShare();
};

/** Subscribes to new incoming native share payloads. */
export const listenForAndroidShares = async (
  listener: (payload: AndroidSharePayload) => void
): Promise<PluginListenerHandle | undefined> => {
  if (!isAndroidShareSupported()) return undefined;
  const AndroidShareHandler = await getAndroidShareHandler();
  return AndroidShareHandler.addListener('shareReceived', listener);
};

/** Converts a cached native shared file into a browser File for upload. */
export const materializeSharedFile = async (
  sharedFile: AndroidSharedFile,
  receivedAt: number
): Promise<File> => {
  const { Capacitor } = await loadCapacitorCore();
  const fileUrl = Capacitor.convertFileSrc(sharedFile.path);
  const response = await fetch(fileUrl);
  if (!response.ok) {
    throw new Error(`Failed to read shared file: ${sharedFile.name}`);
  }

  const blob = await response.blob();
  return new File([blob], sharedFile.name, {
    type: sharedFile.mimeType || blob.type,
    lastModified: receivedAt,
  });
};