<script setup lang="ts">
import Button from 'primevue/button';
import { invoke } from '../invoke';
import { Package } from '../types';
import { needsUpdate, pkgKey } from '../util';

const props = defineProps({
    pkg: Object as () => Package,
});

const install = async () => {
    if (props.pkg === undefined) {
        return;
    }

    try {
        await invoke('install_package', {
            key: pkgKey(props.pkg),
            force: true,
        });
    } catch (err) {
        if (props.pkg !== undefined) {
            props.pkg.js.busy = false;
        }
    }

    //if (rv === 'Deferred') { /* download progress */ }
};
</script>

<template>
    <Button
        v-if="needsUpdate(pkg) && !pkg?.js.busy"
        rounded
        icon="pi pi-download"
        severity="success"
        aria-label="remove"
        size="small"
        class="self-center mr-4"
        style="width: 2rem; height: 2rem"
        v-on:click="install()"
    />
</template>
