import {createRouter, createWebHashHistory} from 'vue-router';
import Home from './views/Home.vue';
import Product from './views/Product.vue';
import CategoriesList from "./views/CategoriesList.vue";
import Cart from "./views/Cart.vue";
import Products from "@/views/Products.vue";
import Checkout from "@/views/Checkout.vue";
import OrderCreated from "@/views/OrderCreated.vue";
import Search from "@/views/Search.vue";
import Filters from "@/views/Filters.vue";
import {useYaMetrikaStore} from "@/stores/yaMetrikaStore.js";

const routes = [
    {
        path: '/',
        name: 'home',
        component: Home,
    },
    {path: '/filters', name: 'filters', component: Filters},
    {path: '/product/:id', name: 'product.show', component: Product},
    {
        path: '/products/:category_id',
        name: 'product.categories.show',
        component: Products,
    },
    {path: '/categories', name: 'categories', component: CategoriesList},
    {path: '/category/:id', name: 'category.show', component: CategoriesList},
    {path: '/cart', name: 'cart', component: Cart},
    {path: '/checkout', name: 'checkout', component: Checkout},
    {path: '/success', name: 'order_created', component: OrderCreated},
    {path: '/search', name: 'search', component: Search},
];

export const router = createRouter({
    history: createWebHashHistory('/image/catalog/tgshopspa/'),
    routes,
    scrollBehavior(to, from, savedPosition) {
        if (savedPosition) {
            return savedPosition;
        }

        return {top: 0, behavior: 'smooth'};
    },
});

router.beforeEach((to, from, next) => {
    const ym = useYaMetrikaStore();
    ym.prevPath = from.path;
    next();
});
