"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get segmentItemConfig () {
        return segmentItemConfig;
    },
    get segmentItemRoot () {
        return segmentItemRoot;
    }
});
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _utils = require("../../../../utils");
const _tokens = require("../../tokens");
const _SegmentProvider = require("../../SegmentProvider/SegmentProvider");
const _SegmentGroupstyles = require("../SegmentGroup/SegmentGroup.styles");
const _base = require("./variations/_size/base");
const _base1 = require("./variations/_view/base");
const _base2 = require("./variations/_disabled/base");
const _base3 = require("./variations/_pilled/base");
const _SegmentItemstyles = require("./SegmentItem.styles");
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
const segmentItemRoot = (Root)=>/*#__PURE__*/ (0, _react.forwardRef)((props, outerRef)=>{
        const { style, size, view = 'default', className, id, label, value, pilled, contentLeft, contentRight, maxItemWidth: maxWidth = 'auto', customHandleSelect, 'aria-label': ariaLabelExternal, ...rest } = props;
        const { disabledGroup, handleSelect, selectedSegmentItems, hasDivider, orientation } = (0, _SegmentProvider.useSegmentInner)();
        const uniqId = (0, _utils.safeUseId)();
        const segmentId = id || `label-${uniqId}`;
        const ariaLabelDefault = (0, _react.useMemo)(()=>(0, _utils.extractTextFrom)(label), [
            label
        ]);
        const pilledClass = pilled ? _tokens.classes.segmentPilled : undefined;
        const xsSize = size === 'xs' ? _tokens.classes.segmentXsSize : undefined;
        const truncateClass = maxWidth !== 'auto' ? _tokens.classes.segmentTruncate : undefined;
        const isSelected = selectedSegmentItems?.includes(value || ariaLabelDefault);
        const selectedClass = isSelected ? _tokens.classes.selectedSegmentItem : undefined;
        const handleSelectSegment = (event)=>{
            if (disabledGroup) {
                return;
            }
            customHandleSelect?.(event);
            handleSelect?.(value || ariaLabelDefault);
        };
        return /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, hasDivider && /*#__PURE__*/ _react.default.createElement(_SegmentGroupstyles.StyledDivider, {
            orientation: _SegmentProvider.dividerOrientationMap[orientation]
        }), /*#__PURE__*/ _react.default.createElement(Root, {
            view: view,
            size: size,
            id: segmentId,
            ref: outerRef,
            "aria-label": ariaLabelExternal || ariaLabelDefault,
            value: value,
            pilled: pilled,
            className: (0, _utils.cx)(_tokens.classes.segmentItem, selectedClass, pilledClass, xsSize, truncateClass, className),
            onClick: handleSelectSegment,
            tabIndex: disabledGroup ? -1 : 0,
            disabled: disabledGroup,
            style: {
                ...style,
                maxWidth
            },
            ...rest
        }, contentLeft && /*#__PURE__*/ _react.default.createElement(_SegmentItemstyles.LeftContent, null, contentLeft), /*#__PURE__*/ _react.default.createElement(_SegmentItemstyles.StyledContent, null, label || value), contentRight && /*#__PURE__*/ _react.default.createElement(_SegmentItemstyles.RightContent, {
            className: typeof contentRight === 'string' ? _tokens.classes.segmentAdditionalText : ''
        }, contentRight)));
    });
const segmentItemConfig = {
    name: 'SegmentItem',
    tag: 'button',
    layout: segmentItemRoot,
    base: _SegmentItemstyles.base,
    variations: {
        size: {
            css: _base.base
        },
        view: {
            css: _base1.base
        },
        disabled: {
            css: _base2.base,
            attrs: true
        },
        pilled: {
            css: _base3.base,
            attrs: true
        }
    },
    defaults: {
        view: 'default',
        size: 'xs'
    }
};
