<script setup lang="ts">
// imports

import useGetAccount from "~/composables/api/account/useGetAccount";

// state

const route = useRoute();

const profileLinks = ref([
    {
        icon: "bi:person-vcard",
        title: "پروفایل",
        path: { name: "profile" },
    },
    {
        icon: "bi:map",
        title: "آدرس ها",
        path: { name: "profile-addresses" },
    },
    {
        icon: "bi:cart",
        title: "خرید ها و سفارش ها",
        path: { name: "profile-purchases-and-orders" },
    },
    {
        icon: "bi:ticket",
        title: "تیکت ها",
        path: { name: "profile-tickets" },
        matchPattern: /^profile-ticket/,
    },
]);

const isLinkActive = (link: any) => {
    if (link.matchPattern) {
        return link.matchPattern.test(route.name);
    } else {
        return route.name === link.path.name;
    }
};

// queries

const { data: account, suspense } = useGetAccount();

await suspense();
</script>

<template>
    <div class="w-3/12 flex flex-col gap-4">
        <div class="w-full rounded-xl bg-slate-50 border border-slate-200 p-4">
            <div class="flex items-center justify-between w-full">
                <div class="flex flex-col items-start gap-1">
                    <span class="flex font-semibold text-dynamic-primary">
                        <p v-if="account?.first_name">
                            {{ `${account?.first_name} ${account?.last_name}` }}
                        </p>
                        <p v-else>بدون نام کاربری</p>
                    </span>
                    <NuxtLink
                        :to="{ name: 'profile' }"
                        class="text-xs font-semibold text-cyan-500"
                    >
                        ویرایش اطلاعات
                    </NuxtLink>
                </div>
                <Avatar
                    class="!size-[3rem]"
                    :src="account!.profile_photo"
                    :alt="
                        account?.first_name && account?.last_name
                            ? `${account?.first_name.charAt(
                                  0
                              )} ${account?.last_name.charAt(0)}`
                            : 'بدون نام کاربری'
                    "
                />
            </div>
        </div>

        <div
            class="w-full flex flex-col gap-2 rounded-xl bg-slate-50 border border-slate-200 p-4"
        >
            <NuxtLink
                v-for="(link, index) in profileLinks"
                :key="index"
                :to="{ ...link.path }"
                :class="
                    isLinkActive(link)
                        ? 'bg-black text-slate-100 **:fill-slate-100'
                        : '**:fill-black'
                "
                class="flex items-center justify-between transition-all rounded-lg py-4 px-3"
            >
                <span class="flex-center gap-3">
                    <div class="size-5 flex-center">
                        <Icon :name="link.icon" />
                    </div>
                    <span class="text-sm">{{ link.title }}</span>
                </span>

                <Icon name="bi:chevron-left" class="transition-all" />
            </NuxtLink>
        </div>
    </div>
</template>

<style scoped></style>
