function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
function _object_spread(target) {
    for(var i = 1; i < arguments.length; i++){
        var source = arguments[i] != null ? arguments[i] : {};
        var ownKeys = Object.keys(source);
        if (typeof Object.getOwnPropertySymbols === "function") {
            ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
                return Object.getOwnPropertyDescriptor(source, sym).enumerable;
            }));
        }
        ownKeys.forEach(function(key) {
            _define_property(target, key, source[key]);
        });
    }
    return target;
}
function _object_without_properties(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys, key, i;
    if (typeof Reflect !== "undefined" && Reflect.ownKeys) {
        sourceKeys = Reflect.ownKeys(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
        return target;
    }
    target = _object_without_properties_loose(source, excluded);
    if (Object.getOwnPropertySymbols) {
        sourceKeys = Object.getOwnPropertySymbols(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
    }
    return target;
}
function _object_without_properties_loose(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys = Object.getOwnPropertyNames(source), key, i;
    for(i = 0; i < sourceKeys.length; i++){
        key = sourceKeys[i];
        if (excluded.indexOf(key) >= 0) continue;
        if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
        target[key] = source[key];
    }
    return target;
}
import React, { forwardRef } from "react";
import { IconClose } from "../_Icon/Icons/IconClose";
import { cx } from "../../utils";
import { base as viewCSS } from "./variations/_view/base";
import { base as sizeCSS } from "./variations/_size/base";
import { base as disabledCSS } from "./variations/_disabled/base";
import { base as readonlyCSS } from "./variations/_readonly/base";
import { base as focusedCSS } from "./variations/_focused/base";
import { base as pilledCSS } from "./variations/_pilled/base";
import { StyledContentClear, StyledContentLeft, StyledContentMain, StyledContentRight, base } from "./Chip.styles";
import { classes, tokens } from "./Chip.tokens";
export var chipRoot = function chipRoot(Root) {
    return /*#__PURE__*/ forwardRef(function(props, ref) {
        var children = props.children, text = props.text, contentLeft = props.contentLeft, contentRight = props.contentRight, contentClearButton = props.contentClearButton, _props_hasClear = props.hasClear, hasClear = _props_hasClear === void 0 ? true : _props_hasClear, size = props.size, className = props.className, onClear = props.onClear, onClick = props.onClick, onClickClose = props.onClickClose, _props_pilled = props.pilled, pilled = _props_pilled === void 0 ? false : _props_pilled, _props_readOnly = props.readOnly, readOnly = _props_readOnly === void 0 ? false : _props_readOnly, _props_disabled = props.disabled, disabled = _props_disabled === void 0 ? false : _props_disabled, // @ts-ignore
        _forceChipManipulationWithReadonly = props._forceChipManipulationWithReadonly, // @ts-ignore
        chipClickArea = props.chipClickArea, rest = _object_without_properties(props, [
            "children",
            "text",
            "contentLeft",
            "contentRight",
            "contentClearButton",
            "hasClear",
            "size",
            "className",
            "onClear",
            "onClick",
            "onClickClose",
            "pilled",
            "readOnly",
            "disabled",
            "_forceChipManipulationWithReadonly",
            "chipClickArea"
        ]);
        var txt = !text && typeof children === 'string' ? children : text;
        var pilledClass = pilled ? classes.pilled : undefined;
        var handleClick = function handleClick(event) {
            // TODO: #1547
            // eslint-disable-next-line no-underscore-dangle
            if (disabled || readOnly && !_forceChipManipulationWithReadonly) {
                return;
            }
            onClick === null || onClick === void 0 ? void 0 : onClick(event);
        };
        var handleClickClose = function handleClickClose(event) {
            if (disabled || readOnly && !_forceChipManipulationWithReadonly || !onClickClose) {
                return;
            }
            event.stopPropagation();
            onClickClose(event);
            if (onClear) {
                onClear();
            }
        };
        return /*#__PURE__*/ React.createElement(Root, _object_spread({
            type: "button",
            ref: ref,
            className: cx(pilledClass, classes.chipItem, className),
            tabIndex: readOnly ? -1 : 0,
            onClick: handleClick,
            disabled: disabled,
            readOnly: !disabled && readOnly && !_forceChipManipulationWithReadonly,
            size: size
        }, rest), contentLeft && /*#__PURE__*/ React.createElement(StyledContentLeft, null, contentLeft), txt ? /*#__PURE__*/ React.createElement(StyledContentMain, null, txt) : children, contentRight && /*#__PURE__*/ React.createElement(StyledContentRight, {
            hasClear: hasClear
        }, contentRight), hasClear && (contentClearButton || /*#__PURE__*/ React.createElement(StyledContentClear, {
            onClick: handleClickClose
        }, /*#__PURE__*/ React.createElement(IconClose, {
            sizeCustomProperty: tokens.closeIconSize,
            color: "inherit"
        }))));
    });
};
export var chipConfig = {
    name: 'Chip',
    tag: 'button',
    layout: chipRoot,
    base: base,
    variations: {
        view: {
            css: viewCSS
        },
        size: {
            css: sizeCSS
        },
        disabled: {
            css: disabledCSS,
            attrs: true
        },
        readOnly: {
            css: readonlyCSS,
            attrs: true
        },
        focused: {
            css: focusedCSS
        },
        pilled: {
            css: pilledCSS,
            attrs: true
        }
    },
    defaults: {
        view: 'default',
        size: 'm'
    }
};
