import useXComponentConfig from "../_utils/hooks/use-x-component-config.js";
import { XActionsCopy } from "../actions/ActionsCopy.js";
import localeValues from "../locale/en_US.js";
import useLocale from "../locale/useLocale.js";
import code_highlighter_default from "../code-highlighter/index.js";
import style_default from "./style/index.js";
import { Fragment, computed, createVNode, defineComponent } from "vue";
import { Empty, Spin } from "antdv-next";
//#region components/folder/FilePreview.tsx
function getFileExtension(path = "") {
	const parts = path.split(".");
	return parts[parts.length - 1] || "";
}
function getLanguageFromExtension(ext) {
	return ext.toLowerCase() || "txt";
}
var FilePreview = /* @__PURE__ */ defineComponent({
	name: "AxFolderFilePreview",
	props: {
		prefixCls: {
			type: String,
			required: true
		},
		classes: {
			type: Object,
			default: () => ({})
		},
		styles: {
			type: Object,
			default: () => ({})
		},
		style: {
			type: Object,
			default: void 0
		},
		selectedFile: {
			type: Array,
			default: () => []
		},
		fileContent: {
			type: String,
			default: ""
		},
		loading: {
			type: Boolean,
			default: false
		},
		previewTitle: {
			type: [
				Boolean,
				String,
				Number,
				Object,
				Array,
				Function
			],
			default: void 0
		},
		previewRender: {
			type: [
				String,
				Number,
				Object,
				Array,
				Function
			],
			default: void 0
		},
		emptyRender: {
			type: [
				Boolean,
				String,
				Number,
				Object,
				Array,
				Function
			],
			default: void 0
		},
		getFileNode: {
			type: Function,
			default: void 0
		}
	},
	setup(props) {
		const [locale] = useLocale("Folder", localeValues.Folder);
		const contextConfig = useXComponentConfig("folder");
		const [hashId, cssVarCls] = style_default(computed(() => props.prefixCls));
		const previewCls = computed(() => `${props.prefixCls}-preview`);
		const renderOriginContent = (fileContent, language) => createVNode(code_highlighter_default, {
			"content": fileContent.replace(/\n$/, ""),
			"language": language,
			"showLanguage": false,
			"showLineNumbers": false,
			"showCopyButton": false,
			"style": {
				background: "transparent",
				border: 0,
				borderRadius: 0,
				padding: 0
			}
		}, null);
		const renderEmpty = () => {
			if (props.emptyRender === false || props.emptyRender === null) return null;
			const node = typeof props.emptyRender === "function" ? props.emptyRender() : props.emptyRender ?? createVNode(Empty, {
				"image": Empty.PRESENTED_IMAGE_SIMPLE,
				"description": locale.value.selectFile
			}, null);
			return createVNode("div", {
				"class": [`${previewCls.value}-empty-container`, props.classes?.previewRender],
				"style": props.styles?.previewRender
			}, [node]);
		};
		const renderContent = () => {
			if (props.loading) return createVNode("div", {
				"class": [`${previewCls.value}-loading-container`, props.classes?.previewRender],
				"style": props.styles?.previewRender
			}, [createVNode(Spin, null, null)]);
			if (!props.selectedFile || props.selectedFile.length === 0) return renderEmpty();
			const selected = props.selectedFile;
			const fileNode = props.getFileNode?.(selected);
			const title = fileNode?.title ?? selected[selected.length - 1];
			const fileName = selected[selected.length - 1];
			const language = getLanguageFromExtension(getFileExtension(fileName));
			let headerNode;
			if (props.previewTitle === false || props.previewTitle === null) headerNode = null;
			else if (props.previewTitle) headerNode = typeof props.previewTitle === "function" ? props.previewTitle({
				title,
				path: selected,
				content: props.fileContent ?? ""
			}) : props.previewTitle;
			else headerNode = createVNode("div", { "class": `${previewCls.value}-title` }, [createVNode("span", { "class": `${previewCls.value}-filename` }, [title]), createVNode(XActionsCopy, {
				"text": props.fileContent ?? "",
				"class": `${previewCls.value}-copy`
			}, null)]);
			const originNode = renderOriginContent(props.fileContent ?? "", language);
			let contentNode;
			if (props.previewRender !== void 0 && props.previewRender !== null) if (typeof props.previewRender === "function") contentNode = props.previewRender({
				content: props.fileContent,
				path: selected,
				title: fileNode?.title,
				language
			}, { originNode });
			else contentNode = props.previewRender;
			else contentNode = originNode;
			return createVNode(Fragment, null, [headerNode && createVNode("div", {
				"class": [`${previewCls.value}-title-wrapper`, props.classes?.previewTitle],
				"style": props.styles?.previewTitle
			}, [headerNode]), createVNode("div", {
				"class": [`${previewCls.value}-content`, props.classes?.previewRender],
				"style": props.styles?.previewRender
			}, [contentNode])]);
		};
		return () => createVNode("div", {
			"class": [
				`${previewCls.value}`,
				props.classes?.filePreview,
				hashId.value,
				cssVarCls.value
			],
			"style": [
				contextConfig.value.styles?.filePreview,
				props.styles?.filePreview,
				props.style
			]
		}, [renderContent()]);
	}
});
//#endregion
export { FilePreview as default };
