<script setup>
import { onBeforeMount } from 'vue';
import { useRouter, onBeforeRouteUpdate, onBeforeRouteLeave } from 'vue-router';
import { useArrayData } from 'src/composables/useArrayData';
import { useStateStore } from 'stores/useStateStore';
import useCardSize from 'src/composables/useCardSize';
import VnSubToolbar from '../ui/VnSubToolbar.vue';

const props = defineProps({
    dataKey: { type: String, required: true },
    url: { type: String, default: undefined },
    idInWhere: { type: Boolean, default: false },
    filter: { type: Object, default: () => {} },
    descriptor: { type: Object, required: true },
    filterPanel: { type: Object, default: undefined },
    searchDataKey: { type: String, default: undefined },
    searchbarProps: { type: Object, default: undefined },
    redirectOnError: { type: Boolean, default: false },
});

const stateStore = useStateStore();
const router = useRouter();
const arrayData = useArrayData(props.dataKey, {
    url: props.url,
    userFilter: props.filter,
    oneRecord: true,
});

onBeforeRouteLeave(() => {
    stateStore.cardDescriptorChangeValue(null);
});

onBeforeMount(async () => {
    stateStore.cardDescriptorChangeValue(props.descriptor);

    const route = router.currentRoute.value;
    try {
        await fetch(route.params.id);
    } catch {
        const { matched: matches } = route;
        const { path } = matches.at(-1);
        router.push({ path: path.replace(/:id.*/, '') });
    }
});

onBeforeRouteUpdate(async (to, from) => {
    if (hasRouteParam(to.params)) {
        const { matched } = router.currentRoute.value;
        const { name } = matched.at(-3);
        if (name) {
            router.push({ name, params: to.params });
        }
    }
    const id = to.params.id;
    if (id !== from.params.id) await fetch(id, true);
});

async function fetch(id, append = false) {
    const regex = /\/(\d+)/;
    if (props.idInWhere) arrayData.store.filter.where = { id };
    else if (!regex.test(props.url)) arrayData.store.url = `${props.url}/${id}`;
    else arrayData.store.url = props.url.replace(regex, `/${id}`);
    await arrayData.fetch({ append, updateRouter: false });
}
function hasRouteParam(params, valueToCheck = ':addressId') {
    return Object.values(params).includes(valueToCheck);
}
</script>
<template>
    <VnSubToolbar />
    <div :class="[useCardSize(), $attrs.class]">
        <RouterView :key="$route.path" />
    </div>
</template>
