<template>
    <nav aria-label="Breadcrumb" class="flex">
        <ol class="inline-flex items-center gap-1">
            <template v-for="(breadcrumb, index) in breadcrumbs">
                <li
                    v-if="index < breadcrumbs.length - 1"
                    class="inline-flex items-center gap-1 text-gray-200"
                >
                    <component
                        :is="breadcrumb.url ? 'Link' : 'span'"
                        :href="breadcrumb.url"
                        class="inline-flex items-center gap-1 font-medium text-gray-200 capitalize md:gap-2"
                    >
                        <svg
                            v-if="breadcrumb.name == 'home'"
                            class="w-4 h-4"
                            fill="currentColor"
                            viewBox="0 0 20 20"
                            xmlns="http://www.w3.org/2000/svg"
                        >
                            <path
                                d="M10.707 2.293a1 1 0 00-1.414 0l-7 7a1 1 0 001.414 1.414L4 10.414V17a1 1 0 001 1h2a1 1 0 001-1v-2a1 1 0 011-1h2a1 1 0 011 1v2a1 1 0 001 1h2a1 1 0 001-1v-6.586l.293.293a1 1 0 001.414-1.414l-7-7z"
                            ></path>
                        </svg>
                        {{ $t(breadcrumb?.name) }}
                    </component>
                    <svg
                        class="w-6 h-6 rtl:rotate-180"
                        fill="currentColor"
                        viewBox="0 0 20 20"
                        xmlns="http://www.w3.org/2000/svg"
                    >
                        <path
                            clip-rule="evenodd"
                            d="M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z"
                            fill-rule="evenodd"
                        ></path>
                    </svg>
                </li>
            </template>
            <li v-if="breadcrumbs.length > 1" aria-current="page">
                <component
                    :is="
                        breadcrumbs[breadcrumbs.length - 1].url
                            ? 'Link'
                            : 'span'
                    "
                    :href="breadcrumbs[breadcrumbs.length - 1].url"
                    class="flex items-center"
                >
                    <span class="font-medium text-blue-300 capitalize">
                       {{ $t(breadcrumbs[breadcrumbs.length - 1].name) }} 
                    </span>
                </component>
            </li>
        </ol>
    </nav>
</template>

<script>
import { Link } from "@inertiajs/inertia-vue3";

const constructBreadcrumbs = () => {
    // get url path
    const paths = window.location.pathname.split("/");
    const breadcrumbs = [];
    paths.forEach((path, index) => {
        const url = paths.slice(0, index + 1).join("/");
        breadcrumbs.push({
            name: path,
            url: url,
        });
    });
    //remove first empty element
    breadcrumbs.shift();
    return breadcrumbs;
};
export default {
    props: {
        breadcrumbs: {
            type: Array,
            default: () => constructBreadcrumbs(),
        },
    },
    methods: {},
    components: {
        Link,
    },
};
</script>
