import { _objectSpread2 } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _objectWithoutProperties } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js";
import useEngineContext from "../../hooks/useEngineContext.js";
import DeviceIconGroup from "./components/deviceIconGroup/index.js";
import SearchSection from "./components/searchSection/index.js";
import locales_default from "./locales.js";
import React, { forwardRef, useImperativeHandle } from "react";
import { Button, Empty, Spin } from "antd";
import { locales } from "@pisell/utils";
import { PisellContainer } from "@pisell/materials";
import { PlusCircle } from "@pisell/icon";
import "./index.less";
//#region src/pro/comprehensiveSearch/index.tsx
const _excluded = [
	"dataSource",
	"isShowScanner",
	"isShowCamera",
	"isShowNFC",
	"isShowNewButton",
	"newButtonAlign",
	"newButtonText",
	"renderType",
	"containerProps",
	"isShowSearch",
	"isShowSearchCamera",
	"searchIcon",
	"placeholder",
	"isShowSearchButton",
	"resultDisplay",
	"resultSort",
	"style",
	"loading",
	"keyword",
	"onKeywordChange",
	"onSearch",
	"onAdd",
	"onRowClick"
];
const ComprehensiveSearch = forwardRef((_ref, ref) => {
	var _context$appHelper;
	let { dataSource = [], isShowScanner = true, isShowCamera = true, isShowNFC = true, isShowNewButton = true, newButtonAlign = "left", newButtonText, renderType, containerProps = {}, isShowSearch = true, isShowSearchCamera = true, searchIcon = "pisell2-search-lg", placeholder, isShowSearchButton = true, resultDisplay = "list", resultSort = "asc", style, loading, keyword, onKeywordChange, onSearch, onAdd, onRowClick } = _ref, props = _objectWithoutProperties(_ref, _excluded);
	const context = useEngineContext();
	locales.init(locales_default, ((_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 || (_context$appHelper = _context$appHelper.utils) === null || _context$appHelper === void 0 || (_context$appHelper = _context$appHelper.storage) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.get("umi_locale")) || "en");
	const handleAdd = () => {
		onAdd === null || onAdd === void 0 || onAdd();
	};
	const renderSearchSection = () => {
		if (!isShowSearch) return null;
		return /* @__PURE__ */ React.createElement(SearchSection, {
			value: keyword,
			onChange: onKeywordChange,
			onSearch,
			loading,
			placeholder
		});
	};
	useImperativeHandle(ref, () => ({}));
	const renderResults = () => {
		if (React.isValidElement(resultDisplay)) return resultDisplay;
		if (typeof resultDisplay === "function") {
			const result = resultDisplay(dataSource);
			if (React.isValidElement(result)) return result;
			return null;
		}
		if (loading) return /* @__PURE__ */ React.createElement(Spin, null, /* @__PURE__ */ React.createElement("div", { className: "comprehensive-search__loading" }));
		if (dataSource.length === 0 && props.__designMode !== "design") return /* @__PURE__ */ React.createElement(Empty, {
			description: locales.getText("pisell2.comprehensive-search.empty"),
			className: "comprehensive-search__empty"
		});
		const resultClass = resultDisplay === "list" ? "comprehensive-search__results--list" : "comprehensive-search__results--grid";
		const renderAllFields = (result) => {
			return Object.entries(result).map(([key, value]) => {
				if (value == null) return null;
				let displayValue;
				if (Array.isArray(value)) displayValue = value.join(", ");
				else if (typeof value === "object") displayValue = JSON.stringify(value);
				else displayValue = String(value);
				return /* @__PURE__ */ React.createElement("div", {
					key,
					className: "comprehensive-search__result-field"
				}, /* @__PURE__ */ React.createElement("span", { className: "comprehensive-search__result-field-value" }, displayValue));
			});
		};
		return /* @__PURE__ */ React.createElement("div", { className: `comprehensive-search__results ${resultClass}` }, dataSource.map((result, index) => /* @__PURE__ */ React.createElement("div", {
			key: result.id,
			className: "comprehensive-search__result-item"
		}, /* @__PURE__ */ React.createElement("div", {
			className: "comprehensive-search__result-fields",
			onClick: () => onRowClick === null || onRowClick === void 0 ? void 0 : onRowClick(result, index)
		}, renderAllFields(result)))));
	};
	const getTopRowLayout = () => {
		const newButton = /* @__PURE__ */ React.createElement(Button, {
			className: "comprehensive-search__new-button",
			onClick: handleAdd,
			type: "primary"
		}, newButtonText || locales.getText("pisell2.text.new-client"), /* @__PURE__ */ React.createElement(PlusCircle, { style: { fontSize: 20 } }));
		const innerProps = {
			isShowCamera,
			isShowNFC,
			isShowScanner
		};
		return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("div", { className: "comprehensive-search__third" }, newButtonAlign === "left" ? newButton : newButtonAlign === "right" ? /* @__PURE__ */ React.createElement(DeviceIconGroup, innerProps) : null), /* @__PURE__ */ React.createElement("div", { className: "comprehensive-search__third" }, newButtonAlign === "center" ? newButton : null), /* @__PURE__ */ React.createElement("div", { className: "comprehensive-search__third" }, newButtonAlign === "left" ? /* @__PURE__ */ React.createElement(DeviceIconGroup, innerProps) : newButtonAlign === "right" ? newButton : /* @__PURE__ */ React.createElement(DeviceIconGroup, innerProps)));
	};
	const renderContent = () => /* @__PURE__ */ React.createElement("div", _objectSpread2({
		className: "comprehensive-search",
		style
	}, props), /* @__PURE__ */ React.createElement("div", { className: "comprehensive-search__top-section" }, /* @__PURE__ */ React.createElement("div", { className: "comprehensive-search__top-row" }, getTopRowLayout())), renderSearchSection(), /* @__PURE__ */ React.createElement("div", { className: "comprehensive-search__results-container" }, renderResults()));
	return /* @__PURE__ */ React.createElement(PisellContainer, {
		renderType,
		props: _objectSpread2({
			open: true,
			cancelVisible: false,
			bodyStyle: {
				padding: "0px 24px 24px",
				overflow: "hidden",
				display: "flex",
				flexDirection: "column"
			},
			width: 680,
			onCancel: () => {},
			okButtonProps: {},
			onOk: () => {},
			footer: null,
			headerDivider: false,
			className: "comprehensive-search-modal"
		}, containerProps)
	}, renderContent());
});
//#endregion
export { ComprehensiveSearch as default };
