"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "TextFieldChipNew", {
    enumerable: true,
    get: function() {
        return TextFieldChipNew;
    }
});
var _react = /*#__PURE__*/ _interop_require_default(require("react"));
var _TextFieldChipstyles = require("../TextFieldChip/TextFieldChip.styles");
var _TextFieldstyles = require("../../TextField.styles");
var _TextFieldtokens = require("../../TextField.tokens");
var _TextFieldChipNewstyles = require("./TextFieldChipNew.styles");
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 _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: 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 ownKeys(object, enumerableOnly) {
    var keys = Object.keys(object);
    if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);
        if (enumerableOnly) {
            symbols = symbols.filter(function(sym) {
                return Object.getOwnPropertyDescriptor(object, sym).enumerable;
            });
        }
        keys.push.apply(keys, symbols);
    }
    return keys;
}
function _object_spread_props(target, source) {
    source = source != null ? source : {};
    if (Object.getOwnPropertyDescriptors) {
        Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
    } else {
        ownKeys(Object(source)).forEach(function(key) {
            Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
        });
    }
    return target;
}
var TextFieldChipNew = function TextFieldChipNew(param) {
    var chips = param.chips, onChipCloseClick = param.onChipCloseClick, getRef = param.getRef, handleChipKeyDown = param.handleChipKeyDown, onChipClear = param.onChipClear, readOnly = param.readOnly, chipType = param.chipType, view = param.view, _param_chipClickArea = param.chipClickArea, chipClickArea = _param_chipClickArea === void 0 ? 'full' : _param_chipClickArea, _forceChipManipulationWithReadonly = param._forceChipManipulationWithReadonly;
    return /*#__PURE__*/ _react.default.createElement(_TextFieldstyles.StyledChips, {
        className: _TextFieldtokens.classes.chipsWrapper
    }, chips.map(function(param, index) {
        var value = param.value, label = param.label, disabled = param.disabled;
        var chipId = "".concat(index, "_").concat(value);
        var handleChipClick = function handleChipClick(event) {
            event.stopPropagation();
        };
        var onKeyDownHandle = function onKeyDownHandle(event) {
            if (handleChipKeyDown) {
                handleChipKeyDown(event, chipId, index);
            }
            if (event.key === 'Enter' || event.key === 'Backspace') {
                if (onChipCloseClick) {
                    onChipCloseClick({
                        value: value,
                        label: label,
                        disabled: disabled
                    });
                }
            }
        };
        var handleCloseClick = function handleCloseClick(event) {
            event.stopPropagation();
            if (onChipCloseClick) {
                onChipCloseClick({
                    value: value,
                    label: label,
                    disabled: disabled
                });
            }
            if (onChipClear) {
                onChipClear(chipId, index);
            }
        };
        return chipType === 'default' ? /*#__PURE__*/ _react.default.createElement(_TextFieldChipNewstyles.StyledChip, _object_spread_props(_object_spread({
            key: chipId,
            ref: function ref(element) {
                return getRef(element, index);
            },
            tabIndex: -1,
            disabled: disabled,
            hasClear: !disabled,
            readOnly: readOnly,
            onKeyDown: onKeyDownHandle,
            onClear: function onClear() {
                return onChipClear(chipId, index);
            },
            text: label,
            view: view,
            chipClickArea: chipClickArea
        }, chipClickArea === 'full' ? {
            onClick: handleCloseClick
        } : {
            onClick: handleChipClick,
            onClickClose: handleCloseClick
        }), {
            // TODO: #1547
            // eslint-disable-next-line @typescript-eslint/ban-ts-comment
            // @ts-ignore
            _forceChipManipulationWithReadonly: _forceChipManipulationWithReadonly
        })) : /*#__PURE__*/ _react.default.createElement(_TextFieldChipstyles.TextChip, {
            tabIndex: -1,
            onClick: handleCloseClick,
            onKeyDown: onKeyDownHandle
        }, label);
    }));
};
