"use strict";
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 __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var jsx_runtime_1 = require("react/jsx-runtime");
var antd_1 = require("antd");
var react_1 = require("react");
var register_1 = __importDefault(require("../register"));
var entries_1 = require("./entries");
var A_1 = __importDefault(require("components/misc/A"));
var icon_1 = require("@cocalc/frontend/components/icon");
var misc_1 = require("@cocalc/util/misc");
var router_1 = require("next/router");
var path_1 = require("path");
function Search(_a) {
    var allowEmpty = _a.allowEmpty;
    var _b = __read((0, react_1.useState)(""), 2), value = _b[0], setValue = _b[1];
    var _c = __read((0, react_1.useState)((0, entries_1.search)(value, allowEmpty)), 2), results = _c[0], setResults = _c[1];
    var 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: function (e) {
                    setValue(e.target.value);
                    onSearch(e.target.value);
                }, onPressEnter: function () {
                    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: function () { return setValue(""); } }))] }));
}
exports.default = Search;
(0, register_1.default)({
    path: "search/input",
    title: "Search",
    desc: "",
    icon: "search",
    Component: function () { return (0, jsx_runtime_1.jsx)(Search, { allowEmpty: true }); },
});
function SearchResults(_a) {
    var results = _a.results, onClick = _a.onClick;
    var router = (0, router_1.useRouter)();
    return ((0, jsx_runtime_1.jsx)(antd_1.List, { style: { marginBottom: "15px" }, bordered: true, itemLayout: "horizontal", dataSource: results, renderItem: function (item) {
            var top = item.path.split("/")[0];
            return ((0, jsx_runtime_1.jsx)(A_1.default, __assign({ title: item.title, onClick: function () {
                    onClick();
                    router.push((0, path_1.join)("/config", item.path));
                } }, { children: (0, jsx_runtime_1.jsx)(antd_1.List.Item, __assign({ 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", __assign({ style: { color: "darkblue" } }, { children: item.title }))] }), description: item.desc }) })) })));
        } }));
}
//# sourceMappingURL=component.js.map