<svelte:head>
    <link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet">
</svelte:head>

<script lang="ts">
    import Home from "./routes/Home.svelte";
    import PackageLists from "./routes/PackageLists.svelte";
    import PackageList from "./routes/PackageList.svelte";

    function normalize(path) {
        return path.replace(/\/+$/, '') + "/";
    }

    let currentRoute;
    let data;

    function route(path) {
        path = normalize(path);
        console.log(`Routing path "${path}"`);
        data = {}

        let urlParts = path.split("/").slice(1, -1);

        if (path === "/") {
            console.log("=> Home");
            currentRoute = Home;
        } else if (urlParts[0] == "lists" && urlParts.length == 1) {
            console.log("=> PackageLists");
            currentRoute = PackageLists;
        } else if (urlParts[0] == "lists" && urlParts.length == 2) {
            console.log("=> PackageList");
            currentRoute = PackageList;
            data = {id: urlParts[1]};
        } else {
            console.log("No matching route found");
        }
    }

    window.onload = e => {
        route(window.location.pathname);
    }

    function redirect(path) {
        history.pushState({id: path}, "", path);
        route(path);
    }

    window.addEventListener("locationchange", function() {
        route(window.location.pathname);
    });

    window.addEventListener("popstate", event => {
        route(window.location.pathname);
    });

</script>

<main>
    <svelte:component this={currentRoute} redirect={redirect} data={data}/>
</main>
