import { _asyncToGenerator } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import useEngineContext from "../../../../hooks/useEngineContext.js";
import AppearanceShow from "../AppearanceShow/index.js";
import AppearanceEdit_default from "../AppearanceEdit/index.js";
import Modal from "../Modal/index.js";
import { antdConfirmPromise } from "../Modal/antdConfirmPromise.js";
import { items } from "./constants.js";
import EditableString from "../EditableString/index.js";
import React, { useMemo, useRef, useState } from "react";
import { isFunction } from "@pisell/utils";
import { Dropdown, Input, Popover, Select, Space, Switch } from "@pisell/materials";
import { EllipsisOutlined } from "@ant-design/icons";
import "./index.less";
//#region src/components/workSpaceList/components/Card/index.tsx
const stopPropagation = (e) => {
	e.stopPropagation();
};
const titleMap = {
	duplicate: () => `Duplicate "Untitled Base"`,
	move: "Move Untitled Base to another workspace"
};
const DuplicateContent = () => {
	return /* @__PURE__ */ React.createElement("div", { className: "duplicate-content-wrap" }, /* @__PURE__ */ React.createElement("div", { className: "appearance-wrap" }, /* @__PURE__ */ React.createElement(AppearanceShow, {
		icon: "Un",
		color: "#7F56D9",
		size: 72,
		fontSize: 30
	})), /* @__PURE__ */ React.createElement(Input, null), /* @__PURE__ */ React.createElement(Space, { className: "duplicate-records" }, /* @__PURE__ */ React.createElement(Switch, { defaultChecked: true }), /* @__PURE__ */ React.createElement("span", null, "Duplicate records")));
};
const MoveContent = () => {
	return /* @__PURE__ */ React.createElement("div", { className: "move-content-wrap" }, /* @__PURE__ */ React.createElement("div", null, "Choose workspace"), /* @__PURE__ */ React.createElement(Select, {
		className: "select",
		options: [{
			label: "Workspace 1",
			value: 1
		}, {
			label: "Workspace 2",
			value: 2
		}]
	}));
};
const Card$1 = (props) => {
	const { name, describe, created_at, icon, color, id, onDeleteBase, onUpdateBase } = props;
	const { pushV2AdminPage } = useEngineContext().appHelper.utils.getApp().history;
	const [editing, setEditing] = useState(false);
	const [open, setOpen] = useState(false);
	const [popoverOpen, setPopoverOpen] = useState(false);
	const [currentType, setCurrentType] = useState("");
	const cardRef = useRef(null);
	const appearanceEditRef = useRef(null);
	const onClick = function() {
		var _ref = _asyncToGenerator(function* ({ key, domEvent }) {
			domEvent.stopPropagation();
			if (key === "rename") setEditing(true);
			else if (key === "share") {} else if (key === "duplicate") {
				setOpen(true);
				setCurrentType("duplicate");
			} else if (key === "move") {
				setOpen(true);
				setCurrentType("move");
			} else if (key === "customize") setPopoverOpen(true);
			else if (key === "settings") {} else if (key === "delete") {
				yield antdConfirmPromise({
					type: "confirm",
					title: "Are you sure you want to delete Untitled Base?",
					content: "This operation can cause all data in the Base to be deleted.",
					okText: "Delete base"
				});
				onDeleteBase === null || onDeleteBase === void 0 || onDeleteBase(id);
			}
		});
		return function onClick(_x) {
			return _ref.apply(this, arguments);
		};
	}();
	const handleNameChange = (val) => {
		setEditing(false);
		if (name === val) return;
		onUpdateBase === null || onUpdateBase === void 0 || onUpdateBase({
			id,
			name: val
		});
	};
	const handleOk = () => {};
	const title = useMemo(() => {
		const result = titleMap[currentType];
		return isFunction(result) ? result() : result;
	}, [currentType]);
	const content = useMemo(() => {
		return /* @__PURE__ */ React.createElement(AppearanceEdit_default, {
			name,
			color,
			icon,
			isShowName: false,
			colorTitle: "Edit icon appearance",
			ref: appearanceEditRef
		});
	}, [
		name,
		color,
		icon
	]);
	const handleOpenChange = function() {
		var _ref2 = _asyncToGenerator(function* (o) {
			if (!o) {
				var _appearanceEditRef$cu;
				setPopoverOpen(o);
				const detail = (_appearanceEditRef$cu = appearanceEditRef.current) === null || _appearanceEditRef$cu === void 0 ? void 0 : _appearanceEditRef$cu.getDetail();
				const newBase = {
					id,
					color: detail === null || detail === void 0 ? void 0 : detail.color,
					icon: (detail === null || detail === void 0 ? void 0 : detail.icon) || "",
					name: detail === null || detail === void 0 ? void 0 : detail.name
				};
				if (JSON.stringify(newBase) === JSON.stringify({
					id,
					color,
					icon: icon || "",
					name
				})) return;
				onUpdateBase === null || onUpdateBase === void 0 || onUpdateBase({
					id,
					color: detail === null || detail === void 0 ? void 0 : detail.color,
					icon: detail === null || detail === void 0 ? void 0 : detail.icon,
					name: detail === null || detail === void 0 ? void 0 : detail.name
				});
			}
		});
		return function handleOpenChange(_x2) {
			return _ref2.apply(this, arguments);
		};
	}();
	const handleCardClick = () => {
		pushV2AdminPage(`/lowcode/${id}`);
	};
	return /* @__PURE__ */ React.createElement(Popover, {
		placement: "bottomLeft",
		content,
		open: popoverOpen,
		arrow: false,
		onOpenChange: handleOpenChange,
		destroyTooltipOnHide: true,
		trigger: "click"
	}, /* @__PURE__ */ React.createElement("div", {
		className: "card-wrap",
		ref: cardRef,
		onClick: handleCardClick
	}, /* @__PURE__ */ React.createElement("div", null, /* @__PURE__ */ React.createElement(AppearanceShow, {
		icon,
		color,
		name,
		size: 72,
		fontSize: 30
	})), /* @__PURE__ */ React.createElement("div", { className: "right" }, /* @__PURE__ */ React.createElement("div", { className: "line-1" }, /* @__PURE__ */ React.createElement("span", { onClick: editing && stopPropagation }, /* @__PURE__ */ React.createElement(EditableString, {
		editing,
		editable: false,
		text: name || "",
		onChange: handleNameChange
	})), /* @__PURE__ */ React.createElement(Dropdown, {
		menu: {
			items,
			onClick
		},
		placement: "bottomRight"
	}, /* @__PURE__ */ React.createElement("span", {
		className: "operate-wrap",
		onClick: stopPropagation
	}, /* @__PURE__ */ React.createElement(EllipsisOutlined, null)))), /* @__PURE__ */ React.createElement("div", { className: "line-2" }, created_at)), /* @__PURE__ */ React.createElement(Modal, {
		open,
		title,
		width: 400,
		evenlyButton: true,
		onCancel: () => setOpen(false),
		onOk: handleOk
	}, currentType === "duplicate" && /* @__PURE__ */ React.createElement(DuplicateContent, null), currentType === "move" && /* @__PURE__ */ React.createElement(MoveContent, null))));
};
//#endregion
export { Card$1 as default };
