"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");
/*
Note: The TreeSelect component we use below has numerous accessibility
issues, which are being tracked upstream:

    https://github.com/ant-design/ant-design/issues/22343

This component significantly degrades our lighthouse accessibility
score for this page.
*/
const react_1 = require("react");
const api_1 = __importDefault(require("lib/hooks/api"));
const loading_1 = __importDefault(require("components/share/loading"));
const antd_1 = require("antd");
const util_1 = require("./util");
function RecentFiles({ interval, onChange }) {
    const { result, error } = (0, api_1.default)("file-access", {
        interval: interval ?? "1 day",
    });
    const [treeData, setTreeData] = (0, react_1.useState)([]);
    (0, react_1.useEffect)(() => {
        if (!result)
            return;
        // organize the files by project:
        const projects = {};
        for (const file of result.files ?? []) {
            if (projects[file.project_id] == null) {
                projects[file.project_id] = [file];
            }
            else {
                projects[file.project_id].push(file);
            }
        }
        const treeData = [];
        for (const project_id in projects) {
            const files = projects[project_id];
            if (files.length == 0)
                continue;
            const children = [];
            treeData.push({
                title: ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: ["Project: ", (0, jsx_runtime_1.jsx)("b", { children: files[0].title })] })),
                value: files[0].project_id,
                children,
            });
            for (const file of files) {
                children.push({
                    title: file.path,
                    value: file.project_id + file.path,
                });
            }
        }
        setTreeData(treeData);
    }, [result]);
    return ((0, jsx_runtime_1.jsxs)("div", { children: [error && (0, jsx_runtime_1.jsx)(antd_1.Alert, { type: "error", message: error }), result == null ? ((0, jsx_runtime_1.jsx)(loading_1.default, {})) : ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: (0, jsx_runtime_1.jsx)(antd_1.TreeSelect, { style: { width: "100%" }, treeData: treeData, placeholder: (0, jsx_runtime_1.jsx)(util_1.Placeholder, { children: "Search for relevant files..." }), allowClear: true, multiple: true, treeDefaultExpandAll: true, showSearch: true, dropdownStyle: { maxHeight: 400, overflow: "auto" }, onChange: (selected) => {
                        if (!onChange)
                            return;
                        const v = [];
                        for (const value of selected) {
                            const project_id = value.slice(0, 36);
                            if (value.length <= 36) {
                                v.push({ project_id });
                            }
                            else {
                                const path = value.slice(36);
                                v.push({ project_id, path });
                            }
                        }
                        onChange(v);
                    } }) }))] }));
}
exports.default = RecentFiles;
//# sourceMappingURL=recent-files.js.map