'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var React = require('react');
var Chip = require('../../../Chip/Chip.js');
var utils = require('../../utils.js');
var styles = require('./styles.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);

function Selectors({ selectors, handleChange, narrow, selectedSelector, 'data-testid': dataTestId, }) {
    const handleClick = (value) => () => {
        const selector = selectors === null || selectors === void 0 ? void 0 : selectors.find((s) => s.value === value);
        if (selector)
            handleChange === null || handleChange === void 0 ? void 0 : handleChange(selector);
    };
    return (React__default['default'].createElement(styles.SelectorsWrapper, { narrow: !!narrow, "data-testid": dataTestId }, selectors === null || selectors === void 0 ? void 0 : selectors.map((selector) => (React__default['default'].createElement(Chip.Chip, { key: selector.label, label: selector.label || '', selected: selector.value === selectedSelector.value, disabled: selector.disabled, variant: "primary", "data-testid": `${dataTestId}-${selector.value}`, onClick: handleClick(selector.value) })))));
}
/**
 * @deprecated Prefer using Selectors component above. This is kept until next major release for backwards compatibility
 */
function SelectorsOld(props) {
    var _a;
    const defaultVals = props.defaultSelectorValues && Object.entries(props.defaultSelectorValues);
    const commonSelectors = utils.getSelectors(utils.commonDateSelectors);
    const defaults = commonSelectors.reduce((prev, curr) => {
        if (defaultVals) {
            const valueToUpdate = defaultVals.find((d) => d[0].toUpperCase() === curr.value);
            if (valueToUpdate === null || valueToUpdate === void 0 ? void 0 : valueToUpdate[1]) {
                const temp = Object.assign(Object.assign({}, curr), { label: valueToUpdate[1].label });
                return [...prev, temp];
            }
            else if (valueToUpdate && valueToUpdate[1] === null) {
                return [...prev];
            }
        }
        return [...prev, curr];
    }, []);
    function onClick(value, isDefault) {
        if (isDefault) {
            const selector = commonSelectors.find((selector) => selector.value === value);
            selector && props.handleChange(selector);
        }
        else if (props.customSelectors) {
            const selector = props.customSelectors.find((selector) => selector.value === value);
            selector && props.handleChange(selector);
        }
    }
    return (React__default['default'].createElement(styles.SelectorsWrapper, { narrow: !!props.narrow, "data-testid": props['data-testid'] },
        defaults.map((selector) => {
            return (React__default['default'].createElement(Chip.Chip, { key: selector.label, label: selector.label || '', selected: selector.value === props.selectedSelector.value, disabled: selector.disabled, variant: "primary", "data-testid": `${props['data-testid']}-${selector.value}`, onClick: () => onClick(selector.value, true) }));
        }), (_a = props.customSelectors) === null || _a === void 0 ? void 0 :
        _a.map((selector) => {
            return (React__default['default'].createElement(Chip.Chip, { key: selector.label, label: selector.label, selected: selector.value === props.selectedSelector.value, disabled: selector.disabled, variant: "primary", "data-testid": `${props['data-testid']}-${selector.value}`, onClick: () => onClick(selector.value, false) }));
        })));
}

exports.Selectors = Selectors;
exports.SelectorsOld = SelectorsOld;
