<script lang="ts">
	import { appState } from "@/stores"
	import { IconEnum, type AppInfo } from "@kksh/api/models"
	import { Command } from "@kksh/svelte5"
	import { IconMultiplexer } from "@kksh/ui"
	import { DraggableCommandGroup } from "@kksh/ui/custom"
	import { convertFileSrc } from "@tauri-apps/api/core"
	import { getCurrentWindow } from "@tauri-apps/api/window"
	import * as os from "@tauri-apps/plugin-os"
	import { toast } from "svelte-sonner"
	import { executeBashScript, open } from "tauri-plugin-shellx-api"

	const platform = os.platform()
	let { apps }: { apps: AppInfo[] } = $props()
</script>

<DraggableCommandGroup heading="Apps">
	{#each apps.filter((app) => app.name) as app, idx}
		{@const iconPath = platform === "windows" ? (app.icon_path ?? app.app_path_exe) : app.icon_path}
		<Command.Item
			class="flex justify-between"
			onSelect={async () => {
				if (platform === "windows") {
					if (app.app_path_exe) {
						open(app.app_path_exe)
					} else {
						toast.error("No executable path found for this app")
					}
				} else if (platform === "macos") {
					open(app.app_desktop_path)
				} else if (platform === "linux") {
					if (app.app_path_exe) {
						executeBashScript(app.app_path_exe)
					} else {
						toast.error("No executable path found for this app")
					}
				} else {
					toast.error("Unsupported platform")
				}
				await getCurrentWindow().hide()
				appState.clearSearchTerm()
			}}
			value={`app:${idx}:${app.app_desktop_path}`}
		>
			<span class="flex gap-2">
				<IconMultiplexer
					icon={iconPath
						? {
								type: IconEnum.RemoteUrl,
								value: convertFileSrc(iconPath, "appicon")
							}
						: {
								type: IconEnum.Iconify,
								value: "mdi:application"
							}}
					class="!h-5 !w-5 shrink-0"
				/>
				<span>{app.name}</span>
				<!-- <span>{app.app_path_exe}</span> -->
			</span>
		</Command.Item>
	{/each}
</DraggableCommandGroup>
