"use client";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { FileIcon, ImageOff } from "lucide-react";
import { cn } from "../../lib/utils";
const IMAGE_EXTENSIONS = [
    "jpg",
    "jpeg",
    "png",
    "gif",
    "bmp",
    "webp",
    "avif",
    "svg",
    "heic",
    "heif",
];
const getBaseUrl = () => {
    if (typeof window !== "undefined" && window.location) {
        return window.location.origin;
    }
    return "http://localhost";
};
function getExtension(url) {
    if (!url)
        return undefined;
    try {
        const parsed = new URL(url, getBaseUrl());
        const pathname = parsed.pathname;
        const lastSegment = pathname.split("/").pop();
        const name = lastSegment?.split("?")[0] ?? "";
        const ext = name.split(".").pop();
        return ext?.toLowerCase();
    }
    catch {
        const match = url.match(/\.([a-zA-Z0-9]+)(?:\?|$)/);
        return match?.[1]?.toLowerCase();
    }
}
function getFileName(url) {
    if (!url)
        return undefined;
    try {
        const parsed = new URL(url, getBaseUrl());
        const pathname = parsed.pathname;
        const name = pathname.split("/").pop();
        return decodeURIComponent(name ?? "");
    }
    catch {
        const lastSegment = url.split("/").pop();
        const name = lastSegment?.split("?")[0] ?? "";
        return name ? decodeURIComponent(name) : undefined;
    }
}
export function AttachmentEditor({ field, readOnly }) {
    const src = field?.value?.src;
    const ext = field?.value?.extension?.toLowerCase() ?? getExtension(src);
    const extension = ext;
    const isImage = ext ? IMAGE_EXTENSIONS.includes(ext) : false;
    const fileName = field?.value?.fileName ?? getFileName(src);
    if (!src) {
        return (_jsxs("div", { className: "flex min-h-24 items-center justify-center rounded border border-dashed border-gray-300 bg-gray-50 p-4 text-xs text-gray-500", children: [_jsx(ImageOff, { className: "mr-2 h-4 w-4", strokeWidth: 1 }), " No attachment selected"] }));
    }
    if (isImage) {
        return (_jsx("div", { className: cn("focus-shadow flex max-h-72 w-full max-w-xs items-center justify-center overflow-hidden rounded border border-gray-300 bg-gray-50", readOnly && "opacity-80"), "data-testid": "attachment-field-image", children: _jsx("img", { src: src, alt: fileName ?? "Attachment", className: "max-h-72 w-full object-contain", loading: "lazy" }) }));
    }
    return (_jsx("div", { className: "focus-shadow flex flex-col items-start gap-2 rounded border border-gray-300 bg-gray-50 p-3 text-xs", children: _jsxs("div", { className: "flex items-center gap-2", children: [_jsx(FileIcon, { className: "h-4 w-4", strokeWidth: 1 }), _jsx("span", { className: "break-all", children: fileName ?? src }), extension && (_jsx("span", { className: "rounded bg-gray-200 px-1.5 py-0.5 text-[10px] text-gray-600 uppercase", children: extension }))] }) }));
}
//# sourceMappingURL=AttachmentEditor.js.map