'use strict';

var React = require('react');
var Button = require('./Button-cjs.js');
require('classnames');
var Chip = require('./Chip-cjs.js');
require('./tslib.es6-cjs.js');
require('@jobber/design');
var ComboboxProvider = require('./ComboboxProvider-cjs.js');
var constants = require('./constants-cjs.js');

function useComboboxActivatorAccessibility() {
    const { label, open } = React.useContext(ComboboxProvider.ComboboxContext);
    return {
        ariaLabel: label,
        ariaExpanded: open,
        ariaControls: constants.COMBOBOX_MENU_ID,
        role: "combobox",
    };
}

function ComboboxActivator(props) {
    const { handleOpen } = React.useContext(ComboboxProvider.ComboboxContext);
    const accessibilityAttributes = useComboboxActivatorAccessibility();
    if (React.isValidElement(props.children) &&
        (props.children.type === Button.Button || props.children.type === Chip.Chip)) {
        return React.cloneElement(props.children, {
            role: accessibilityAttributes.role,
            onClick: handleOpen,
        });
    }
    else if (typeof props.children === "function") {
        return props.children(Object.assign(Object.assign({}, accessibilityAttributes), { open: handleOpen }));
    }
    return null;
}

exports.ComboboxActivator = ComboboxActivator;
