import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import dayjs from "dayjs";
import LocalizedFormat from "dayjs/plugin/localizedFormat";
import RelativeTime from "dayjs/plugin/relativeTime";
import { shortId } from "../../../utils";
import { Eye } from "lucide-react";
import { Button } from "@vertesia/ui/core";
dayjs.extend(RelativeTime);
dayjs.extend(LocalizedFormat);
const renderers = {
    string(params, _onClick) {
        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, _onClick) {
        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, _onClick) {
        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);
        };
    },
    objectId(params, onClick) {
        let transforms = [];
        let hasSlice = false;
        if (params) {
            const slice = params.get("slice");
            if (slice) {
                hasSlice = true;
                transforms.push((value) => value.slice(parseInt(slice)));
            }
        }
        return (value, index) => {
            const displayValue = transforms.reduce((v, t) => t(v), value.id);
            return (_jsxs("td", { className: "flex justify-between items-center gap-2", children: [hasSlice ? '~' : '', displayValue, _jsx(Button, { variant: "ghost", alt: "Preview Object", onClick: (e) => {
                            e.stopPropagation();
                            onClick?.(value.id);
                        }, children: _jsx(Eye, { className: "size-4" }) })] }, index));
        };
    },
    objectName(params, _onClick) {
        let title = "title";
        if (params) {
            title = params.get("title") || "title";
        }
        return (value, index) => {
            return (_jsx("td", { children: value.properties?.[title] || value.name || shortId(value.id) }, index));
        };
    },
    typeLink(_params, _onClick) {
        return (value, index) => {
            return _jsx("td", { children: value?.name || "n/a" }, index);
        };
    },
    date(params, _onClick) {
        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