"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const jsx_runtime_1 = require("react/jsx-runtime");
const antd_1 = require("antd");
const react_1 = require("react");
const register_1 = __importDefault(require("../register"));
const entries_1 = require("./entries");
const A_1 = __importDefault(require("components/misc/A"));
const icon_1 = require("@cocalc/frontend/components/icon");
const misc_1 = require("@cocalc/util/misc");
const router_1 = require("next/router");
const path_1 = require("path");
function Search({ allowEmpty }) {
    const [value, setValue] = (0, react_1.useState)("");
    const [results, setResults] = (0, react_1.useState)((0, entries_1.search)(value, allowEmpty));
    const router = (0, router_1.useRouter)();
    function onSearch(value) {
        setResults((0, entries_1.search)(value));
    }
    return ((0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)(antd_1.Input.Search, { autoFocus: allowEmpty, style: { maxWidth: "60ex" }, placeholder: "Search all configuration options (use /re/ for regexp)...", onSearch: onSearch, enterButton: true, allowClear: true, value: value, onChange: (e) => {
                    setValue(e.target.value);
                    onSearch(e.target.value);
                }, onPressEnter: () => {
                    if (results != null && results.length > 0) {
                        // visit first search result.
                        router.push((0, path_1.join)("/config", results[0].path));
                        setValue("");
                    }
                } }), (0, jsx_runtime_1.jsx)("br", {}), (0, jsx_runtime_1.jsx)("br", {}), results != null && (allowEmpty || value.trim()) && ((0, jsx_runtime_1.jsx)(SearchResults, { results: results, onClick: () => setValue("") }))] }));
}
exports.default = Search;
(0, register_1.default)({
    path: "search/input",
    title: "Search",
    desc: "",
    icon: "search",
    Component: () => (0, jsx_runtime_1.jsx)(Search, { allowEmpty: true }),
});
function SearchResults({ results, onClick, }) {
    const router = (0, router_1.useRouter)();
    return ((0, jsx_runtime_1.jsx)(antd_1.List, { style: { marginBottom: "15px" }, bordered: true, itemLayout: "horizontal", dataSource: results, renderItem: (item) => {
            const top = item.path.split("/")[0];
            return ((0, jsx_runtime_1.jsx)(A_1.default, { title: item.title, onClick: () => {
                    onClick();
                    router.push((0, path_1.join)("/config", item.path));
                }, children: (0, jsx_runtime_1.jsx)(antd_1.List.Item, { style: { borderBottom: "1px solid lightgrey" }, children: (0, jsx_runtime_1.jsx)(antd_1.List.Item.Meta, { avatar: item.icon ? ((0, jsx_runtime_1.jsx)(icon_1.Icon, { name: item.icon, style: { fontSize: "16pt" } })) : undefined, title: (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, misc_1.capitalize)(top), " ", (0, jsx_runtime_1.jsx)(icon_1.Icon, { name: "arrow-right" }), " ", (0, jsx_runtime_1.jsx)("span", { style: { color: "darkblue" }, children: item.title })] }), description: item.desc }) }) }));
        } }));
}
//# sourceMappingURL=component.js.map