<script setup lang="ts">
import { Ref, ref } from 'vue';
import * as path from '@tauri-apps/api/path';
import OptionCategory from './OptionCategory.vue';
import PatchEntry from './PatchEntry.vue';
import { invoke } from '../invoke';
import { usePrfStore } from '../stores';
import { Patch } from '../types';

const prf = usePrfStore();

prf.reload();

const gamePatches: Ref<Patch[] | null> = ref(null);
const amdPatches: Ref<Patch[] | null> = ref(null);

invoke('list_patches', { target: prf.current!.data.sgt.target }).then(
    (patches) => {
        gamePatches.value = patches as Patch[];
    }
);

(async () => {
    const amd = await path.join(
        prf.current!.data.sgt.target,
        '../amdaemon.exe'
    );
    amdPatches.value = (await invoke('list_patches', {
        target: amd,
    })) as Patch[];
})();

const errorMessage =
    "No compatible patches found. Make sure you're using unpacked and unpatched files.";
</script>

<template>
    <OptionCategory title="chusanApp.exe" always-found>
        <PatchEntry
            v-if="gamePatches !== null"
            v-for="p in gamePatches"
            :patch="p"
        />
        <div v-if="gamePatches === null">Loading...</div>
        <div v-if="gamePatches !== null && gamePatches.length === 0">
            {{ errorMessage }}
        </div>
    </OptionCategory>
    <OptionCategory title="amdaemon.exe" always-found>
        <PatchEntry
            v-if="amdPatches !== null"
            v-for="p in amdPatches"
            :patch="p"
        />
        <div v-if="gamePatches === null">Loading...</div>
        <div v-if="amdPatches !== null && amdPatches.length === 0">
            {{ errorMessage }}
        </div>
    </OptionCategory>
</template>
