'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 FilterPickerProvider = require('./FilterPickerProvider-cjs.js');
var constants = require('./constants-cjs.js');

function useFilterPickerActivatorAccessibility() {
    const { label, open } = React.useContext(FilterPickerProvider.FilterPickerContext);
    return {
        ariaLabel: label,
        ariaExpanded: open,
        ariaControls: constants.FILTER_PICKER_MENU_ID,
        role: "combobox",
    };
}

function FilterPickerActivator(props) {
    const { handleOpen } = React.useContext(FilterPickerProvider.FilterPickerContext);
    const accessibilityAttributes = useFilterPickerActivatorAccessibility();
    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.FilterPickerActivator = FilterPickerActivator;
