<script lang="ts" setup>
import * as pagination from "@zag-js/pagination";
import type { Props } from "@zag-js/pagination";
import { Slot } from "@/components/ui/slot";
import { cn } from "@mykopkb/core/utils/cn";
import { normalizeProps, useMachine } from "@zag-js/vue";
import { computed, provide } from "vue";
import { paginationRoot } from "@mykopkb/core/styles/pagination.styles";

const props = defineProps<Partial<Props> & { class?: string; asChild?: boolean }>();

const paginationId = crypto.randomUUID();

const machineProps = computed(() => {
  const { class: _class, asChild: _asChild, id: _id, ...rest } = props;

  return {
    ...rest,
    id: paginationId,
  };
});

const service = useMachine(pagination.machine, machineProps);
const api = computed(() => pagination.connect(service, normalizeProps));

provide("paginationApi", api);
</script>

<template>
  <Slot :class="cn(paginationRoot, props.class)" v-bind="{ ...$attrs, ...api.getRootProps() }">
    <slot v-if="props.asChild" />
    <div v-else>
      <slot />
    </div>
  </Slot>
</template>
