"use strict";
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 __values = (this && this.__values) || function(o) {
    var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
    if (m) return m.call(o);
    if (o && typeof o.length === "number") return {
        next: function () {
            if (o && i >= o.length) o = void 0;
            return { value: o && o[i++], done: !o };
        }
    };
    throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
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");
/*
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.
*/
var react_1 = require("react");
var api_1 = __importDefault(require("lib/hooks/api"));
var loading_1 = __importDefault(require("components/share/loading"));
var antd_1 = require("antd");
var util_1 = require("./util");
function RecentFiles(_a) {
    var interval = _a.interval, onChange = _a.onChange;
    var _b = (0, api_1.default)("file-access", {
        interval: interval !== null && interval !== void 0 ? interval : "1 day",
    }), result = _b.result, error = _b.error;
    var _c = __read((0, react_1.useState)([]), 2), treeData = _c[0], setTreeData = _c[1];
    (0, react_1.useEffect)(function () {
        var e_1, _a, e_2, _b;
        var _c;
        if (!result)
            return;
        // organize the files by project:
        var projects = {};
        try {
            for (var _d = __values((_c = result.files) !== null && _c !== void 0 ? _c : []), _e = _d.next(); !_e.done; _e = _d.next()) {
                var file = _e.value;
                if (projects[file.project_id] == null) {
                    projects[file.project_id] = [file];
                }
                else {
                    projects[file.project_id].push(file);
                }
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
            }
            finally { if (e_1) throw e_1.error; }
        }
        var treeData = [];
        for (var project_id in projects) {
            var files = projects[project_id];
            if (files.length == 0)
                continue;
            var 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: children,
            });
            try {
                for (var files_1 = (e_2 = void 0, __values(files)), files_1_1 = files_1.next(); !files_1_1.done; files_1_1 = files_1.next()) {
                    var file = files_1_1.value;
                    children.push({
                        title: file.path,
                        value: file.project_id + file.path,
                    });
                }
            }
            catch (e_2_1) { e_2 = { error: e_2_1 }; }
            finally {
                try {
                    if (files_1_1 && !files_1_1.done && (_b = files_1.return)) _b.call(files_1);
                }
                finally { if (e_2) throw e_2.error; }
            }
        }
        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: function (selected) {
                        var e_3, _a;
                        if (!onChange)
                            return;
                        var v = [];
                        try {
                            for (var selected_1 = __values(selected), selected_1_1 = selected_1.next(); !selected_1_1.done; selected_1_1 = selected_1.next()) {
                                var value = selected_1_1.value;
                                var project_id = value.slice(0, 36);
                                if (value.length <= 36) {
                                    v.push({ project_id: project_id });
                                }
                                else {
                                    var path = value.slice(36);
                                    v.push({ project_id: project_id, path: path });
                                }
                            }
                        }
                        catch (e_3_1) { e_3 = { error: e_3_1 }; }
                        finally {
                            try {
                                if (selected_1_1 && !selected_1_1.done && (_a = selected_1.return)) _a.call(selected_1);
                            }
                            finally { if (e_3) throw e_3.error; }
                        }
                        onChange(v);
                    } }) }))] }));
}
exports.default = RecentFiles;
//# sourceMappingURL=recent-files.js.map