import { jsx as _jsx } from "react/jsx-runtime";
import { NavLink } from "@vertesia/ui/router";
import dayjs from "dayjs";
import LocalizedFormat from "dayjs/plugin/localizedFormat";
import RelativeTime from "dayjs/plugin/relativeTime";
import { shortId } from "../../../utils";
dayjs.extend(RelativeTime);
dayjs.extend(LocalizedFormat);
const renderers = {
    string(params) {
        let transforms = [];
        if (params) {
            const slice = params.get("slice");
            if (slice) {
                transforms.push((value) => value.slice(parseInt(slice)));
            }
            const max_length = params.get("max_length");
            if (max_length) {
                transforms.push((value) => value.slice(0, parseInt(max_length)));
            }
            if (params.has("upper")) {
                transforms.push((value) => value.toUpperCase());
            }
            if (params.has("lower")) {
                transforms.push((value) => value.toLowerCase());
            }
            if (params.has("capitalize")) {
                transforms.push((value) => value[0].toUpperCase() + value.substring(1));
            }
            if (params.has("ellipsis")) {
                transforms.push((value) => value + "...");
            }
        }
        return (value, index) => {
            let v;
            if (value) {
                v = String(value);
                if (transforms.length > 0) {
                    for (const t of transforms) {
                        v = t(v);
                    }
                }
            }
            else {
                v = "";
            }
            return _jsx("td", { children: v }, index);
        };
    },
    fileSize(_params) {
        return (value, index) => {
            let fileSize = "";
            if (value) {
                const bytes = Number(value);
                if (!isNaN(bytes)) {
                    const sizes = ["Bytes", "KB", "MB", "GB", "TB"];
                    if (bytes === 0) {
                        fileSize = "0 Bytes";
                    }
                    else {
                        const i = Math.floor(Math.log(bytes) / Math.log(1024));
                        fileSize = `${(bytes / Math.pow(1024, i)).toFixed(2)} ${sizes[i]}`;
                    }
                }
                else {
                    fileSize = String(value);
                }
            }
            return _jsx("td", { children: fileSize }, index);
        };
    },
    number(params) {
        let currency;
        let decimals;
        if (params) {
            currency = params.get("currency") || undefined;
            decimals = params.get("decimals") || undefined;
        }
        const digits = decimals ? parseInt(decimals) : 2;
        return (value, index) => {
            let v = new Intl.NumberFormat("en-US", {
                style: currency ? "currency" : "decimal",
                currency,
                maximumFractionDigits: digits,
            }).format(value);
            return _jsx("td", { children: v }, index);
        };
    },
    // value must be the object itself
    objectLink(params) {
        let title = "title";
        //let underline = "hover";
        if (params) {
            title = params.get("title") || "title";
            //underline = params.get("underline") || "hover";
        }
        return (value, index) => {
            return (_jsx("td", { children: _jsx(NavLink, { topLevelNav: true, className: "underline text-indigo-800 dark:text-indigo-300", href: `/store/objects/${value.id}`, children: value.properties?.[title] || value.name || shortId(value.id) }) }, index));
        };
    },
    typeLink(_params) {
        return (value, index) => {
            return _jsx("td", { children: value?.name || "n/a" }, index);
        };
    },
    date(params) {
        let method = "format";
        let arg = "LLL";
        if (params) {
            const localized = params.get("localized");
            if (localized) {
                arg = localized;
            }
            else {
                const relative = params.get("relative");
                if (relative) {
                    method = relative; // fromNow or toNow
                    arg = undefined;
                }
            }
        }
        return (value, index) => {
            return _jsx("td", { children: dayjs(value)[method](arg) }, index);
        };
    },
};
export default renderers;
//# sourceMappingURL=renderers.js.map