var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
import React from 'react';
import { LeafSelectionTieredSelect } from './LeafSelectionTieredSelect';
import { TierSelectionTieredSelect } from './TierSelectionTieredSelect';
function defaultGetGroupId(tier) {
    return tier.groupId;
}
function defaultGetId(tier) {
    return tier.id;
}
function defaultGetLabel(tier) {
    return tier.label;
}
function defaultGetNextGroupId(tier) {
    return tier.nextGroupId;
}
function getFullPath(getLabel) {
    if (getLabel === void 0) { getLabel = defaultGetLabel; }
    return function getValueString(value) {
        return value.map(function (tier) { return getLabel(tier); }).join(' > ');
    };
}
function getLeafOnly(getLabel) {
    if (getLabel === void 0) { getLabel = defaultGetLabel; }
    return function getValueString(value) {
        return value.length ? getLabel(value[value.length - 1]) : '';
    };
}
function getValueStringFn(valueStringResolution, getLabel) {
    if (getLabel === void 0) { getLabel = defaultGetLabel; }
    if (valueStringResolution === 'full-path') {
        return getFullPath(getLabel);
    }
    if (valueStringResolution === 'leaf-only') {
        return getLeafOnly(getLabel);
    }
    return valueStringResolution;
}
function noop() { }
function always() {
    return true;
}
export var useHighlightItemEffects = function (_a) {
    var currentTier = _a.currentTier, searchValue = _a.searchValue, menuRef = _a.menuRef, loading = _a.loading, loadingMore = _a.loadingMore;
    var _b = React.useState(), previousTier = _b[0], setPreviousTier = _b[1];
    React.useEffect(function () {
        setPreviousTier(currentTier);
    }, [currentTier]);
    var _c = React.useState(''), previousSearch = _c[0], setPreviousSearch = _c[1];
    React.useEffect(function () {
        setPreviousSearch(searchValue);
    }, [searchValue]);
    var currentMenuRef = menuRef.current;
    React.useEffect(function () {
        if (previousSearch !== searchValue || previousTier !== currentTier) {
            currentMenuRef === null || currentMenuRef === void 0 ? void 0 : currentMenuRef.highlightFirst();
        }
    }, [currentMenuRef, currentTier, previousTier, searchValue, previousSearch]);
    React.useEffect(function () {
        if (loadingMore) {
            currentMenuRef === null || currentMenuRef === void 0 ? void 0 : currentMenuRef.highlightLast(false);
        }
    }, [currentMenuRef, loadingMore]);
    React.useEffect(function () {
        if (!loading && !(currentMenuRef === null || currentMenuRef === void 0 ? void 0 : currentMenuRef.highlighted())) {
            currentMenuRef === null || currentMenuRef === void 0 ? void 0 : currentMenuRef.highlightFirst();
        }
    }, [currentMenuRef, loading]);
};
export var TieredSelectContext = React.createContext({
    afterHide: noop,
    afterShow: noop,
    beforeHide: always,
    beforeShow: always,
    getGroupId: defaultGetGroupId,
    getId: defaultGetId,
    getLabel: defaultGetLabel,
    getNextGroupId: defaultGetNextGroupId,
    getValueString: getFullPath(defaultGetLabel),
    i18nScope: 'core.tieredSelect',
    onChange: noop,
});
/**

 We use tiered selects to allow users to choose a single parent or child option
 that is nested within a tiered list. For example, choosing a location from
 within a list of tiered locations. We typically see these selects on forms.

 Other select components are group select, multi select, and select.
 @since 10.19.0

 @see [Storybook](https://procore.github.io/core/latest/?path=/story/tieredselect--basic)

 @see [Design Guidelines](https://design.procore.com/tiered-select)

 */
export var TieredSelect = React.forwardRef(function TieredSelect(_a, ref) {
    var _b = _a.afterHide, afterHide = _b === void 0 ? noop : _b, _c = _a.afterShow, afterShow = _c === void 0 ? noop : _c, _d = _a.beforeHide, beforeHide = _d === void 0 ? always : _d, _e = _a.beforeShow, beforeShow = _e === void 0 ? always : _e, block = _a.block, _f = _a.getGroupId, getGroupId = _f === void 0 ? defaultGetGroupId : _f, _g = _a.getId, getId = _g === void 0 ? defaultGetId : _g, _h = _a.getLabel, getLabel = _h === void 0 ? defaultGetLabel : _h, _j = _a.getNextGroupId, getNextGroupId = _j === void 0 ? defaultGetNextGroupId : _j, _k = _a.getValueString, getValueString = _k === void 0 ? getFullPath(getLabel) : _k, _l = _a.i18nScope, i18nScope = _l === void 0 ? 'core.tieredSelect' : _l, _m = _a.onChange, onChange = _m === void 0 ? noop : _m, onClear = _a.onClear, onNavigate = _a.onNavigate, onQuickCreate = _a.onQuickCreate, onScrollBottom = _a.onScrollBottom, onSearch = _a.onSearch, _o = _a.selectableTiers, selectableTiers = _o === void 0 ? true : _o, tabIndex = _a.tabIndex, props = __rest(_a, ["afterHide", "afterShow", "beforeHide", "beforeShow", "block", "getGroupId", "getId", "getLabel", "getNextGroupId", "getValueString", "i18nScope", "onChange", "onClear", "onNavigate", "onQuickCreate", "onScrollBottom", "onSearch", "selectableTiers", "tabIndex"]);
    var isLeaf = React.useCallback(function (tier) {
        return !getNextGroupId(tier);
    }, [getNextGroupId]);
    return (React.createElement(TieredSelectContext.Provider, { value: {
            afterHide: afterHide,
            afterShow: afterShow,
            beforeHide: beforeHide,
            beforeShow: beforeShow,
            block: block,
            getGroupId: getGroupId,
            getId: getId,
            getLabel: getLabel,
            getNextGroupId: getNextGroupId,
            getValueString: getValueStringFn(getValueString),
            i18nScope: i18nScope,
            isLeaf: isLeaf,
            onChange: onChange,
            onClear: onClear,
            onNavigate: onNavigate,
            onQuickCreate: onQuickCreate,
            onScrollBottom: onScrollBottom,
            onSearch: onSearch,
            tabIndex: tabIndex,
        } }, selectableTiers ? (React.createElement(TierSelectionTieredSelect, __assign({ ref: ref }, props))) : (React.createElement(LeafSelectionTieredSelect, __assign({ ref: ref }, props)))));
});
export default TieredSelect;
//# sourceMappingURL=TieredSelect.js.map