<template>
    <component v-if="!asChild" :is="as" :class="cx('root')" v-bind="attrs" />
    <slot v-else :a11yAttrs="a11yAttrs" :class="cx('root')" />
</template>

<script>
import { cn } from '@primeuix/utils';
import { mergeProps } from 'vue';
import BaseInputColorSliderHandle from './BaseInputColorSliderHandle.vue';

export default {
    name: 'InputColorSliderHandle',
    extends: BaseInputColorSliderHandle,
    inheritAttrs: false,
    inject: ['$pcInputColor', '$pcInputColorSlider'],
    computed: {
        attrs() {
            return mergeProps(this.a11yAttrs, this.ptmi('root'));
        },
        a11yAttrs() {
            const slider = this.$pcInputColorSlider;
            const channel = slider.channel;
            const range = slider.range;
            const value = slider.currentValue;

            return {
                role: 'slider',
                tabindex: this.$pcInputColor.disabled || slider.disabled ? -1 : 0,
                'aria-disabled': this.$pcInputColor.disabled || slider.disabled || undefined,
                'aria-orientation': slider.orientation,
                'aria-label': channel,
                'aria-valuemin': range.min,
                'aria-valuemax': range.max,
                'aria-valuenow': value,
                'aria-valuetext': `${channel} ${value}`,
                'data-p': this.dataP,
                style: slider.handleStyle,
                onKeydown: slider.onSliderKeyDown
            };
        },
        dataP() {
            const slider = this.$pcInputColorSlider;

            return cn({
                disabled: this.$pcInputColor.disabled || slider.disabled,
                dragging: slider.isSliderDragging,
                horizontal: slider.orientation === 'horizontal',
                vertical: slider.orientation === 'vertical'
            });
        }
    }
};
</script>
