"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = (exports1, definition)=>{
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (exports1)=>{
        if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    initialPickerState: ()=>initialPickerState,
    pickerReducer: ()=>pickerReducer,
    useResourcePickerState: ()=>useResourcePickerState
});
const external_react_namespaceObject = require("react");
const index_cjs_namespaceObject = require("../service/index.cjs");
const initialPickerState = {
    anchorPosition: null,
    query: '',
    drillDown: null,
    loading: false,
    loadingMore: false,
    searchInProgress: false,
    error: null,
    searchResults: [],
    searchDone: true,
    previousDisplayCount: 5
};
function pickerReducer(state, action) {
    switch(action.type){
        case 'OPEN':
            return {
                ...initialPickerState,
                anchorPosition: action.coords
            };
        case 'CLOSE':
            return {
                ...state,
                anchorPosition: null
            };
        case 'SET_QUERY':
            {
                const newTrimmed = action.query.trim();
                if (action.query === state.query || newTrimmed === state.query.trim()) return state;
                return {
                    ...state,
                    query: action.query,
                    searchInProgress: !!newTrimmed
                };
            }
        case 'LOAD_START':
            return {
                ...state,
                loading: true,
                error: null
            };
        case 'LOAD_MORE_START':
            return {
                ...state,
                loadingMore: true
            };
        case 'SEARCH_START':
            return {
                ...state,
                searchInProgress: true,
                error: null
            };
        case 'DRILL_DOWN_SUCCESS':
            return {
                ...state,
                loading: false,
                drillDown: {
                    category: action.category,
                    resources: action.resources,
                    done: action.done
                }
            };
        case 'SEARCH_SUCCESS':
            return {
                ...state,
                loading: false,
                searchInProgress: false,
                searchResults: action.results,
                searchDone: action.done,
                previousDisplayCount: action.results.length > 0 ? action.results.length : index_cjs_namespaceObject.CHAT_RESOURCE_PICKER_MIN_SKELETON_COUNT
            };
        case 'LOAD_MORE_SUCCESS':
            if ('drillDown' === action.target) return state.drillDown ? {
                ...state,
                loadingMore: false,
                drillDown: {
                    ...state.drillDown,
                    resources: [
                        ...state.drillDown.resources,
                        ...action.items
                    ],
                    done: action.done
                }
            } : {
                ...state,
                loadingMore: false
            };
            return {
                ...state,
                loadingMore: false,
                searchResults: [
                    ...state.searchResults,
                    ...action.items
                ],
                searchDone: action.done
            };
        case 'LOAD_ERROR':
            return {
                ...state,
                loading: false,
                loadingMore: false,
                searchInProgress: false,
                error: action.error
            };
        case 'GO_BACK':
            return {
                ...state,
                query: '',
                drillDown: null
            };
        case 'CLEAR_SEARCH':
            return {
                ...state,
                searchResults: [],
                searchDone: true,
                searchInProgress: false
            };
        default:
            return state;
    }
}
function useResourcePickerState() {
    return (0, external_react_namespaceObject.useReducer)(pickerReducer, initialPickerState);
}
exports.initialPickerState = __webpack_exports__.initialPickerState;
exports.pickerReducer = __webpack_exports__.pickerReducer;
exports.useResourcePickerState = __webpack_exports__.useResourcePickerState;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "initialPickerState",
    "pickerReducer",
    "useResourcePickerState"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
