'use strict';

var tslib_es6 = require('./tslib.es6-cjs.js');
var React = require('react');
var DataListContext = require('./DataListContext-cjs.js');
var Text = require('./Text-cjs.js');
var Button = require('./Button-cjs.js');
require('classnames');
var DataList_const = require('./DataList.const-cjs.js');

var styles = {"emptyStateWrapper":"R0MWvfTO9vg-","spinning":"hVJ2XoR6bR0-"};

// eslint-disable-next-line @typescript-eslint/no-unused-vars
function DataListEmptyState(_) {
    return null;
}
function InternalDataListEmptyState() {
    const { emptyStateComponents: components, filtered } = React.useContext(DataListContext.DataListContext);
    const _a = getEmptyStateContent(), { customRender } = _a, contentProps = tslib_es6.__rest(_a, ["customRender"]);
    return (React.createElement("div", { className: styles.emptyStateWrapper }, customRender ? (customRender(Object.assign({}, contentProps))) : (React.createElement(React.Fragment, null,
        React.createElement(Text.Text, { align: "center" }, contentProps.message),
        renderButton(contentProps.action)))));
    function getEmptyStateContent() {
        const { defaultEmptyState, filteredEmptyState } = getEmptyStates(components);
        if (filtered) {
            return {
                message: (filteredEmptyState === null || filteredEmptyState === void 0 ? void 0 : filteredEmptyState.props.message) || DataList_const.EMPTY_FILTER_RESULTS_MESSAGE,
                action: filteredEmptyState === null || filteredEmptyState === void 0 ? void 0 : filteredEmptyState.props.action,
                customRender: filteredEmptyState === null || filteredEmptyState === void 0 ? void 0 : filteredEmptyState.props.customRender,
            };
        }
        return {
            message: (defaultEmptyState === null || defaultEmptyState === void 0 ? void 0 : defaultEmptyState.props.message) || DataList_const.EMPTY_RESULTS_MESSAGE,
            action: defaultEmptyState === null || defaultEmptyState === void 0 ? void 0 : defaultEmptyState.props.action,
            customRender: defaultEmptyState === null || defaultEmptyState === void 0 ? void 0 : defaultEmptyState.props.customRender,
        };
    }
}
function renderButton(action) {
    if (action) {
        if (action.type === Button.Button) {
            return React.cloneElement(action, {
                variation: action.props.variation || "subtle",
            });
        }
        throw new Error(DataList_const.EMPTY_STATE_ACTION_BUTTON_ONLY_ERROR);
    }
    return;
}
function getEmptyStates(components) {
    const defaultEmptyState = components === null || components === void 0 ? void 0 : components.find(es => es.props.type === "empty" || es.props.type === undefined);
    const filteredEmptyState = components === null || components === void 0 ? void 0 : components.find(es => es.props.type === "filtered");
    return { defaultEmptyState, filteredEmptyState };
}

exports.DataListEmptyState = DataListEmptyState;
exports.InternalDataListEmptyState = InternalDataListEmptyState;
