import axios from 'axios';
import { useRouter } from 'vue-router';
import { useI18n } from 'vue-i18n';
import { useQuasar } from 'quasar';
import VnConfirm from 'components/ui/VnConfirm.vue';

export function cloneItem() {
    const { t } = useI18n();

    const quasar = useQuasar();
    const router = useRouter();
    const cloneItem = async (entityId) => {
        const { id } = entityId;
        try {
            const { data } = await axios.post(`Items/${id ?? entityId}/clone`);
            router.push({ name: 'ItemTags', params: { id: data.id } });
        } catch (err) {
            console.error('Error cloning item');
        }
    };

    const openCloneDialog = async (entityId) => {
        quasar
            .dialog({
                component: VnConfirm,
                componentProps: {
                    title: t('item.descriptor.clone.title'),
                    message: t('item.descriptor.clone.subTitle'),
                },
            })
            .onOk(async () => {
                await cloneItem(entityId);
            });
    };
    return { openCloneDialog };
}
