"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");
/*
SelectUsers of this Cocalc server.

Inspired by https://ant.design/components/select/#components-select-demo-select-users
*/
const react_1 = require("react");
const antd_1 = require("antd");
const debounce_1 = __importDefault(require("lodash/debounce"));
const post_1 = __importDefault(require("lib/api/post"));
const timestamp_1 = __importDefault(require("components/misc/timestamp"));
const avatar_1 = __importDefault(require("components/account/avatar"));
const icon_1 = require("@cocalc/frontend/components/icon");
function SelectUsers({ autoFocus, exclude, onChange }) {
    const [value, setValue] = (0, react_1.useState)([]);
    return ((0, jsx_runtime_1.jsx)(DebounceSelect, { autoFocus: autoFocus, exclude: new Set(exclude), mode: "multiple", value: value, placeholder: "Email address, name or @username", fetchOptions: fetchUserList, onChange: (newValue) => {
            setValue(newValue);
            onChange?.(newValue.map((x) => x.value));
        }, style: { width: "100%" } }));
}
exports.default = SelectUsers;
function DebounceSelect({ exclude, fetchOptions, debounceTimeout = 800, ...props }) {
    const [fetching, setFetching] = (0, react_1.useState)(false);
    const [error, setError] = (0, react_1.useState)("");
    const [options, setOptions] = (0, react_1.useState)([]);
    const fetchRef = (0, react_1.useRef)(0);
    const debounceFetcher = (0, react_1.useMemo)(() => {
        const loadOptions = async (value) => {
            fetchRef.current += 1;
            const fetchId = fetchRef.current;
            setError("");
            setFetching(true);
            try {
                const newOptions = await fetchOptions(value, exclude);
                if (fetchId == fetchRef.current) {
                    setOptions(newOptions);
                }
            }
            catch (err) {
                setError(err.message);
            }
            finally {
                setFetching(false);
            }
        };
        return (0, debounce_1.default)(loadOptions, debounceTimeout);
    }, [fetchOptions, debounceTimeout]);
    return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [error && ((0, jsx_runtime_1.jsx)(antd_1.Alert, { type: "error", message: error, style: { marginBottom: "15px" } })), (0, jsx_runtime_1.jsx)(antd_1.Select, { labelInValue: true, filterOption: false, onSearch: debounceFetcher, notFoundContent: fetching ? (0, jsx_runtime_1.jsx)(antd_1.Spin, { size: "small" }) : null, ...props, options: options })] }));
}
async function fetchUserList(query, exclude) {
    const v = await (0, post_1.default)("/accounts/search", { query });
    const list = [];
    for (const user of v) {
        if (exclude?.has(user.account_id))
            continue;
        list.push({
            label: (0, jsx_runtime_1.jsx)(Label, { ...user }),
            value: user.account_id,
        });
    }
    return list;
}
function Label({ account_id, first_name, last_name, last_active, created, name, email_address_verified, }) {
    return ((0, jsx_runtime_1.jsxs)("div", { style: { borderBottom: "1px solid lightgrey", paddingBottom: "5px" }, children: [(0, jsx_runtime_1.jsx)(avatar_1.default, { account_id: account_id, size: 18, style: { marginRight: "5px" }, zIndex: 10000 }), first_name, " ", last_name, name ? ` (@${name})` : "", last_active && ((0, jsx_runtime_1.jsxs)("div", { children: ["Last Active: ", (0, jsx_runtime_1.jsx)(timestamp_1.default, { epoch: last_active, dateOnly: true })] })), created && ((0, jsx_runtime_1.jsxs)("div", { children: ["Created: ", (0, jsx_runtime_1.jsx)(timestamp_1.default, { epoch: created, dateOnly: true })] })), email_address_verified && ((0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)(icon_1.Icon, { name: "check", style: { color: "darkgreen" } }), " Email address is verified"] }))] }));
}
//# sourceMappingURL=select-users.js.map