<template>
    <ol :class="cx('root')" v-bind="ptmi('root')" :data-p="dataP">
        <li v-for="(item, index) of value" :key="getKey(item, index)" :class="cx('event')" v-bind="getPTOptions('event', index)" :data-p="dataP">
            <div :class="cx('eventOpposite', { index })" v-bind="getPTOptions('eventOpposite', index)" :data-p="dataP">
                <slot name="opposite" :item="item" :index="index"></slot>
            </div>
            <div :class="cx('eventSeparator')" v-bind="getPTOptions('eventSeparator', index)" :data-p="dataP">
                <slot name="marker" :item="item" :index="index" :icon="item.icon ? resolveIcon(item.icon) : undefined">
                    <div :class="cx('eventMarker')" v-bind="getPTOptions('eventMarker', index)" :data-p="dataP"></div>
                </slot>
                <slot v-if="index !== value.length - 1" name="connector" :item="item" :index="index">
                    <div :class="cx('eventConnector')" v-bind="getPTOptions('eventConnector', index)" :data-p="dataP"></div>
                </slot>
            </div>
            <div :class="cx('eventContent')" v-bind="getPTOptions('eventContent', index)" :data-p="dataP">
                <slot name="content" :item="item" :index="index" :icon="item.icon ? resolveIcon(item.icon) : undefined"></slot>
            </div>
        </li>
    </ol>
</template>

<script>
import { cn } from '@primeuix/utils';
import { isString, resolveFieldData } from '@primeuix/utils/object';
import { toRaw } from 'vue';
import BaseTimeline from './BaseTimeline.vue';

export default {
    name: 'Timeline',
    extends: BaseTimeline,
    inheritAttrs: false,
    methods: {
        getKey(item, index) {
            return this.dataKey ? resolveFieldData(item, this.dataKey) : index;
        },
        getPTOptions(key, index) {
            return this.ptm(key, {
                context: {
                    index,
                    count: this.value.length
                }
            });
        },

        resolveIcon(icon) {
            return isString(icon) ? icon : toRaw(icon);
        },
        isComponentIcon(icon) {
            return !!icon && !isString(icon);
        }
    },
    computed: {
        dataP() {
            return cn({
                [this.layout]: this.layout,
                [this.align]: this.align
            });
        }
    }
};
</script>
