"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 __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 });
exports.renderName = void 0;
var jsx_runtime_1 = require("react/jsx-runtime");
var react_1 = require("react");
var antd_1 = require("antd");
var lodash_1 = require("lodash");
var A_1 = __importDefault(require("components/misc/A"));
var 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) {
    var 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, __assign({ href: url }, { children: name })) : name }), (0, jsx_runtime_1.jsx)("br", {}), record.summary] }));
}
exports.renderName = renderName;
function SoftwareLibraries(props) {
    var e_1, _a;
    var spec = props.spec, inventory = props.inventory, components = props.components, _b = props.libWidthPct, libWidthPct = _b === void 0 ? 60 : _b;
    var dataSource = (0, get_libraries_1.getLibaries)(spec, inventory, components);
    var _c = __read((0, react_1.useState)(""), 2), search = _c[0], setSearch = _c[1];
    var onChange = (0, react_1.useMemo)(function () {
        return (0, lodash_1.debounce)(function (e) {
            setSearch(e.target.value);
        }, 500);
    }, []);
    var data;
    if (!search) {
        data = dataSource;
    }
    else {
        var s = search.toLowerCase();
        data = [];
        try {
            for (var dataSource_1 = __values(dataSource), dataSource_1_1 = dataSource_1.next(); !dataSource_1_1.done; dataSource_1_1 = dataSource_1.next()) {
                var x = dataSource_1_1.value;
                if (x.search.includes(s)) {
                    data.push(x);
                }
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (dataSource_1_1 && !dataSource_1_1.done && (_a = dataSource_1.return)) _a.call(dataSource_1);
            }
            finally { if (e_1) throw e_1.error; }
        }
    }
    var columns = (0, react_1.useMemo)(function () {
        var e_2, _a;
        var envs = Object.entries(spec);
        var width = (100 - libWidthPct) / envs.length;
        var columns = [
            {
                width: "".concat(libWidthPct, "%"),
                title: "Library",
                key: "library",
                dataIndex: "name",
                render: renderName,
            },
        ];
        try {
            for (var envs_1 = __values(envs), envs_1_1 = envs_1.next(); !envs_1_1.done; envs_1_1 = envs_1.next()) {
                var _b = __read(envs_1_1.value, 2), name_1 = _b[0], val = _b[1];
                columns.push({
                    width: "".concat(width, "%"),
                    title: val.name,
                    key: name_1,
                    dataIndex: name_1,
                });
            }
        }
        catch (e_2_1) { e_2 = { error: e_2_1 }; }
        finally {
            try {
                if (envs_1_1 && !envs_1_1.done && (_a = envs_1.return)) _a.call(envs_1);
            }
            finally { if (e_2) throw e_2.error; }
        }
        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: function (e) { return setSearch(e.target.value); } }), (0, jsx_runtime_1.jsx)("div", __assign({ 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