/** tauri-specta globals **/

import { invoke as TAURI_INVOKE } from '@tauri-apps/api';
import * as TAURI_API_EVENT from '@tauri-apps/api/event';
import { type WebviewWindowHandle as __WebviewWindowHandle__ } from '@tauri-apps/api/window';

// This file was generated by [tauri-specta](https://github.com/oscartbeaumont/tauri-specta). Do not edit this file manually.

export const commands = {
	async appReady(): Promise<null> {
		return await TAURI_INVOKE('plugin:tauri-specta|app_ready');
	},
	async resetSpacedrive(): Promise<null> {
		return await TAURI_INVOKE('plugin:tauri-specta|reset_spacedrive');
	},
	async openLogsDir(): Promise<__Result__<null, null>> {
		try {
			return { status: 'ok', data: await TAURI_INVOKE('plugin:tauri-specta|open_logs_dir') };
		} catch (e) {
			if (e instanceof Error) throw e;
			else return { status: 'error', error: e as any };
		}
	},
	async refreshMenuBar(): Promise<__Result__<null, null>> {
		try {
			return {
				status: 'ok',
				data: await TAURI_INVOKE('plugin:tauri-specta|refresh_menu_bar')
			};
		} catch (e) {
			if (e instanceof Error) throw e;
			else return { status: 'error', error: e as any };
		}
	},
	async reloadWebview(): Promise<null> {
		return await TAURI_INVOKE('plugin:tauri-specta|reload_webview');
	},
	async setMenuBarItemState(id: string, enabled: boolean): Promise<null> {
		return await TAURI_INVOKE('plugin:tauri-specta|set_menu_bar_item_state', { id, enabled });
	},
	async requestFdaMacos(): Promise<null> {
		return await TAURI_INVOKE('plugin:tauri-specta|request_fda_macos');
	},
	async openFilePaths(
		library: string,
		ids: number[]
	): Promise<
		__Result__<
			(
				| { t: 'NoLibrary' }
				| { t: 'NoFile'; c: number }
				| { t: 'OpenError'; c: [number, string] }
				| { t: 'AllGood'; c: number }
				| { t: 'Internal'; c: string }
			)[],
			null
		>
	> {
		try {
			return {
				status: 'ok',
				data: await TAURI_INVOKE('plugin:tauri-specta|open_file_paths', { library, ids })
			};
		} catch (e) {
			if (e instanceof Error) throw e;
			else return { status: 'error', error: e as any };
		}
	},
	async openEphemeralFiles(
		paths: string[]
	): Promise<__Result__<({ t: 'Ok'; c: string } | { t: 'Err'; c: string })[], null>> {
		try {
			return {
				status: 'ok',
				data: await TAURI_INVOKE('plugin:tauri-specta|open_ephemeral_files', { paths })
			};
		} catch (e) {
			if (e instanceof Error) throw e;
			else return { status: 'error', error: e as any };
		}
	},
	async getFilePathOpenWithApps(
		library: string,
		ids: number[]
	): Promise<__Result__<{ url: string; name: string }[], null>> {
		try {
			return {
				status: 'ok',
				data: await TAURI_INVOKE('plugin:tauri-specta|get_file_path_open_with_apps', {
					library,
					ids
				})
			};
		} catch (e) {
			if (e instanceof Error) throw e;
			else return { status: 'error', error: e as any };
		}
	},
	async getEphemeralFilesOpenWithApps(
		paths: string[]
	): Promise<__Result__<{ url: string; name: string }[], null>> {
		try {
			return {
				status: 'ok',
				data: await TAURI_INVOKE('plugin:tauri-specta|get_ephemeral_files_open_with_apps', {
					paths
				})
			};
		} catch (e) {
			if (e instanceof Error) throw e;
			else return { status: 'error', error: e as any };
		}
	},
	async openFilePathWith(
		library: string,
		fileIdsAndUrls: [number, string][]
	): Promise<__Result__<null, null>> {
		try {
			return {
				status: 'ok',
				data: await TAURI_INVOKE('plugin:tauri-specta|open_file_path_with', {
					library,
					fileIdsAndUrls
				})
			};
		} catch (e) {
			if (e instanceof Error) throw e;
			else return { status: 'error', error: e as any };
		}
	},
	async openEphemeralFileWith(pathsAndUrls: [string, string][]): Promise<__Result__<null, null>> {
		try {
			return {
				status: 'ok',
				data: await TAURI_INVOKE('plugin:tauri-specta|open_ephemeral_file_with', {
					pathsAndUrls
				})
			};
		} catch (e) {
			if (e instanceof Error) throw e;
			else return { status: 'error', error: e as any };
		}
	},
	async revealItems(library: string, items: RevealItem[]): Promise<__Result__<null, null>> {
		try {
			return {
				status: 'ok',
				data: await TAURI_INVOKE('plugin:tauri-specta|reveal_items', { library, items })
			};
		} catch (e) {
			if (e instanceof Error) throw e;
			else return { status: 'error', error: e as any };
		}
	},
	async lockAppTheme(themeType: AppThemeType): Promise<null> {
		return await TAURI_INVOKE('plugin:tauri-specta|lock_app_theme', { themeType });
	},
	async checkForUpdate(): Promise<
		__Result__<{ version: string; body: string | null } | null, string>
	> {
		try {
			return {
				status: 'ok',
				data: await TAURI_INVOKE('plugin:tauri-specta|check_for_update')
			};
		} catch (e) {
			if (e instanceof Error) throw e;
			else return { status: 'error', error: e as any };
		}
	},
	async installUpdate(): Promise<__Result__<null, string>> {
		try {
			return { status: 'ok', data: await TAURI_INVOKE('plugin:tauri-specta|install_update') };
		} catch (e) {
			if (e instanceof Error) throw e;
			else return { status: 'error', error: e as any };
		}
	}
};

export const events = __makeEvents__<{
	dragAndDropEvent: DragAndDropEvent;
}>({
	dragAndDropEvent: 'plugin:tauri-specta:drag-and-drop-event'
});

/** user-defined types **/

export type AppThemeType = 'Auto' | 'Light' | 'Dark';
export type DragAndDropEvent =
	| { type: 'Hovered'; paths: string[]; x: number; y: number }
	| { type: 'Dropped'; paths: string[]; x: number; y: number }
	| { type: 'Cancelled' };
export type RevealItem =
	| { Location: { id: number } }
	| { FilePath: { id: number } }
	| { Ephemeral: { path: string } };

type __EventObj__<T> = {
	listen: (cb: TAURI_API_EVENT.EventCallback<T>) => ReturnType<typeof TAURI_API_EVENT.listen<T>>;
	once: (cb: TAURI_API_EVENT.EventCallback<T>) => ReturnType<typeof TAURI_API_EVENT.once<T>>;
	emit: T extends null
		? (payload?: T) => ReturnType<typeof TAURI_API_EVENT.emit>
		: (payload: T) => ReturnType<typeof TAURI_API_EVENT.emit>;
};

type __Result__<T, E> = { status: 'ok'; data: T } | { status: 'error'; error: E };

function __makeEvents__<T extends Record<string, any>>(mappings: Record<keyof T, string>) {
	return new Proxy(
		{} as unknown as {
			[K in keyof T]: __EventObj__<T[K]> & {
				(handle: __WebviewWindowHandle__): __EventObj__<T[K]>;
			};
		},
		{
			get: (_, event) => {
				const name = mappings[event as keyof T];

				return new Proxy((() => {}) as any, {
					apply: (_, __, [window]: [__WebviewWindowHandle__]) => ({
						listen: (arg: any) => window.listen(name, arg),
						once: (arg: any) => window.once(name, arg),
						emit: (arg: any) => window.emit(name, arg)
					}),
					get: (_, command: keyof __EventObj__<any>) => {
						switch (command) {
							case 'listen':
								return (arg: any) => TAURI_API_EVENT.listen(name, arg);
							case 'once':
								return (arg: any) => TAURI_API_EVENT.once(name, arg);
							case 'emit':
								return (arg: any) => TAURI_API_EVENT.emit(name, arg);
						}
					}
				});
			}
		}
	);
}
