import { _asyncToGenerator } from "../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import useEngineContext from "../../../../hooks/useEngineContext.js";
import useLocale from "../../../../hooks/useLocale.js";
import useNativeScanner from "../../hooks/useNativeScanner.js";
import locales_default from "./locales.js";
import React, { useMemo } from "react";
import { Button, Input } from "antd";
import { locales } from "@pisell/utils";
import { Icon } from "@pisell/materials";
import classNames from "classnames";
import { useControllableValue, useMemoizedFn } from "ahooks";
import "./index.less";
//#region src/pro/comprehensiveSearch/components/searchSection/index.tsx
const SearchSection = (props) => {
	const { isShowSearchCamera = true, searchIcon = "pisell2-search-lg", placeholder, isShowSearchButton = true, loading = false, defaultValue, value, onChange, onSearch, style, className, searchButtonProps = {}, cameraButtonProps = { icons: ["pisell2-camera-01"] } } = props;
	const { activeNativeScanner, isTerminal } = useNativeScanner();
	useEngineContext();
	const locale = useLocale();
	locales.init(locales_default, locale || "en");
	const [searchValue, setSearchValue] = useControllableValue(props);
	const handleSearch = useMemoizedFn(_asyncToGenerator(function* () {
		try {
			yield onSearch === null || onSearch === void 0 ? void 0 : onSearch(searchValue);
		} catch (e) {
			console.error("Search failed:", e);
		}
	}));
	const handleInputChange = (e) => {
		const value = e.target.value;
		setSearchValue(value);
	};
	const handleCameraClick = () => {
		if (isTerminal) activeNativeScanner();
	};
	const searchButton = useMemo(() => {
		if (!isShowSearchButton) return null;
		return /* @__PURE__ */ React.createElement(Button, {
			className: "comprehensive-search__search-button",
			type: "primary",
			onClick: handleSearch,
			loading,
			style: (searchButtonProps === null || searchButtonProps === void 0 ? void 0 : searchButtonProps.style) || {}
		}, locales.getText("pisell2.text.search"));
	}, [isShowSearchButton, loading]);
	const renderCameraIcons = () => {
		const { icon } = cameraButtonProps;
		if (Array.isArray(icon) && icon.length) return icon.map((item, idx) => {
			let divider = null;
			if (idx > 0) divider = /* @__PURE__ */ React.createElement("span", { className: "comprehensive-search__camera-activator-icons-divider" });
			return /* @__PURE__ */ React.createElement(React.Fragment, null, divider, /* @__PURE__ */ React.createElement(Icon, {
				type: item,
				className: "comprehensive-search__camera-activator-icon"
			}));
		});
		return /* @__PURE__ */ React.createElement(Icon, {
			type: "pisell2-camera-01",
			className: "comprehensive-search__camera-activator-icon"
		});
	};
	return /* @__PURE__ */ React.createElement("div", {
		className: `comprehensive-search__search-section ${className || ""}`,
		style
	}, /* @__PURE__ */ React.createElement("div", { className: "comprehensive-search__search-input-wrapper" }, isShowSearchCamera && /* @__PURE__ */ React.createElement("div", {
		className: classNames("comprehensive-search__camera-activator", { disabled: !isTerminal }),
		onClick: handleCameraClick
	}, renderCameraIcons()), /* @__PURE__ */ React.createElement(Input, {
		value: searchValue,
		onChange: handleInputChange,
		placeholder: placeholder || locales.getText("pisell2.comprehensive-search.placeholder"),
		prefix: /* @__PURE__ */ React.createElement(Icon, {
			className: "comprehensive-search__prefix-icon",
			type: searchIcon
		}),
		onPressEnter: handleSearch,
		className: "comprehensive-search__search-input",
		allowClear: true
	}), (searchButtonProps === null || searchButtonProps === void 0 ? void 0 : searchButtonProps.placement) === "inside" ? searchButton : null), (searchButtonProps === null || searchButtonProps === void 0 ? void 0 : searchButtonProps.placement) === "outside" || !(searchButtonProps === null || searchButtonProps === void 0 ? void 0 : searchButtonProps.placement) ? searchButton : null);
};
//#endregion
export { SearchSection as default };
