<template>
    <div :class="containerClass" :style="style" v-bind="ptmi('root')" :data-p-severity="severity">
        <PVSButton
            type="button"
            :class="cx('pcButton')"
            :disabled="disabled"
            :severity="severity"
            :text="text"
            :outlined="outlined"
            :size="size"
            :fluid="fluid"
            :aria-label="label"
            @click="onDefaultButtonClick"
            v-bind="buttonProps"
            :pt="ptm('pcButton')"
            :unstyled="unstyled"
        >
            <slot name="icon">
                <span v-if="icon" :class="icon" v-bind="ptm('pcButton')['icon']" data-pc-section="buttonicon" />
            </slot>
            <slot>{{ label }}</slot>
        </PVSButton>
        <PVSButton
            ref="button"
            type="button"
            iconOnly
            :class="cx('pcDropdown')"
            :disabled="disabled"
            aria-haspopup="true"
            :aria-expanded="isExpanded"
            :aria-controls="isExpanded ? $id + '_overlay' : undefined"
            @click="onDropdownButtonClick"
            @keydown="onDropdownKeydown"
            :severity="severity"
            :text="text"
            :outlined="outlined"
            :size="size"
            :unstyled="unstyled"
            v-bind="menuButtonProps"
            :pt="ptm('pcDropdown')"
        >
            <slot name="dropdownicon">
                <component :is="menuButtonIcon || dropdownIcon ? 'span' : 'ChevronDown'" :class="dropdownIcon || menuButtonIcon" v-bind="ptm('pcDropdown')['icon']" data-pc-section="menubuttonicon" />
            </slot>
        </PVSButton>
        <PVSMenu ref="menu" :id="$id + '_overlay'" :model="model" :popup="true" :autoZIndex="autoZIndex" :baseZIndex="baseZIndex" :appendTo="appendTo" :unstyled="unstyled" :pt="ptm('pcMenu')">
            <template v-if="$slots.menuitemicon" #itemicon="slotProps">
                <slot name="menuitemicon" :item="slotProps.item" :class="slotProps.class" />
            </template>
            <template v-if="$slots.item" #item="slotProps">
                <slot name="item" :item="slotProps.item" :hasSubmenu="slotProps.hasSubmenu" :label="slotProps.label" :props="slotProps.props"></slot>
            </template>
        </PVSMenu>
    </div>
</template>

<script>
import { isEmpty } from '@primeuix/utils/object';
import ChevronDown from '@primeicons/vue/chevron-down';
import Button from 'primevue/button';
import TieredMenu from 'primevue/tieredmenu';
import BaseSplitButton from './BaseSplitButton.vue';

export default {
    name: 'SplitButton',
    extends: BaseSplitButton,
    inheritAttrs: false,
    emits: ['click'],
    inject: {
        $pcFluid: { default: null }
    },
    data() {
        return {
            isExpanded: false
        };
    },
    mounted() {
        this.$watch('$refs.menu.visible', (newValue) => {
            this.isExpanded = newValue;
        });
    },
    methods: {
        onDropdownButtonClick(event) {
            if (event) {
                event.preventDefault();
            }

            this.$refs.menu.toggle({ currentTarget: this.$el, relatedTarget: this.$refs.button.$el });
            this.isExpanded = this.$refs.menu.visible;
        },
        onDropdownKeydown(event) {
            if (event.code === 'ArrowDown' || event.code === 'ArrowUp') {
                this.onDropdownButtonClick();
                event.preventDefault();
            }
        },
        onDefaultButtonClick(event) {
            if (this.isExpanded) {
                this.$refs.menu.hide(event);
            }

            this.$emit('click', event);
        }
    },
    computed: {
        containerClass() {
            return [this.cx('root'), this.class];
        },
        hasFluid() {
            return isEmpty(this.fluid) ? !!this.$pcFluid : this.fluid;
        }
    },
    components: {
        PVSButton: Button,
        PVSMenu: TieredMenu,
        ChevronDown: ChevronDown
    }
};
</script>
