<template>
    <component v-if="!asChild" :is="as" :class="cx('root')" :type="as === 'BUTTON' || as === 'button' ? 'button' : undefined" v-bind="attrs" @click="onClick" />
    <slot v-else :a11yAttrs="a11yAttrs" :class="cx('root')" :onClick="onClick" />
</template>

<script>
import { mergeProps } from 'vue';
import BaseSidebarRail from './BaseSidebarRail.vue';

export default {
    name: 'SidebarRail',
    extends: BaseSidebarRail,
    inheritAttrs: false,
    inject: {
        $pcSidebar: { default: null }
    },
    methods: {
        onClick(event) {
            this.$pcSidebar?.toggle(event);
        }
    },
    computed: {
        a11yAttrs() {
            const sidebarPart = this.$pcSidebar?.getSectionAttrs('rail') ?? {};

            return {
                ...sidebarPart,
                'data-pc-name': 'sidebarrail',
                'data-pc-section': 'root',
                'aria-label': 'Toggle Sidebar',
                tabIndex: -1
            };
        },
        attrs() {
            return mergeProps(this.a11yAttrs, this.ptmi('root'));
        }
    }
};
</script>
