"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.renderName = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const antd_1 = require("antd");
const lodash_1 = require("lodash");
const A_1 = __importDefault(require("components/misc/A"));
const get_libraries_1 = require("lib/landing/get-libraries");
// check if the string is a URL
function isURL(url) {
    return url && url.match(/^(http|https):\/\//);
}
function renderName(name, record) {
    const url = record.url;
    return ((0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)("b", { children: isURL(url) ? (0, jsx_runtime_1.jsx)(A_1.default, { href: url, children: name }) : name }), (0, jsx_runtime_1.jsx)("br", {}), record.summary] }));
}
exports.renderName = renderName;
function SoftwareLibraries(props) {
    const { spec, inventory, components, libWidthPct = 60 } = props;
    const dataSource = (0, get_libraries_1.getLibaries)(spec, inventory, components);
    const [search, setSearch] = (0, react_1.useState)("");
    const onChange = (0, react_1.useMemo)(() => (0, lodash_1.debounce)((e) => {
        setSearch(e.target.value);
    }, 500), []);
    let data;
    if (!search) {
        data = dataSource;
    }
    else {
        const s = search.toLowerCase();
        data = [];
        for (const x of dataSource) {
            if (x.search.includes(s)) {
                data.push(x);
            }
        }
    }
    const columns = (0, react_1.useMemo)(() => {
        const envs = Object.entries(spec);
        const width = (100 - libWidthPct) / envs.length;
        const columns = [
            {
                width: `${libWidthPct}%`,
                title: "Library",
                key: "library",
                dataIndex: "name",
                render: renderName,
            },
        ];
        for (const [name, val] of envs) {
            columns.push({
                width: `${width}%`,
                title: val.name,
                key: name,
                dataIndex: name,
            });
        }
        return columns;
    }, [libWidthPct]);
    return ((0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsxs)("h2", { children: ["Showing ", data.length, " libraries"] }), (0, jsx_runtime_1.jsx)(antd_1.Input.Search, { style: { padding: "0 30px 15px 0", width: "50%", minWidth: "300px" }, placeholder: "Search...", allowClear: true, onChange: onChange, onPressEnter: (e) => setSearch(e.target.value) }), (0, jsx_runtime_1.jsx)("div", { style: { overflowX: "auto", width: "100%" }, children: (0, jsx_runtime_1.jsx)(antd_1.Table, { columns: columns, bordered: true, pagination: false, rowKey: "index", dataSource: data }) })] }));
}
exports.default = SoftwareLibraries;
//# sourceMappingURL=software-libraries.js.map