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 useTranslationOriginal from "../../hooks/useTranslationOriginal.js";
import comprehensiveSearch_default from "../../pro/comprehensiveSearch/index.js";
import Add_default from "./Add/index.js";
import ResultList from "./ResultList/index.js";
import locales_default from "./locales.js";
import React, { forwardRef, useImperativeHandle, useRef, useState } from "react";
import { locales } from "@pisell/utils";
import "./index.less";
//#region src/plus/clinetSearch/index.tsx
const _excluded = [
	"onRowClick",
	"resultProps",
	"onAddClient",
	"hasMore",
	"onLoadMore",
	"state",
	"resultLoading"
];
const ClientSearch = forwardRef((_ref, ref) => {
	var _context$appHelper, _state$bookingConfig, _context$appHelper2;
	let { onRowClick, resultProps, onAddClient, hasMore, onLoadMore, state, resultLoading } = _ref, props = _objectWithoutProperties(_ref, _excluded);
	const [modalVisible, setModalVisible] = useState(false);
	const comprehensiveSearchRef = useRef(null);
	const addClientRef = useRef(null);
	const resultListRef = useRef(null);
	const translationOriginal = useTranslationOriginal();
	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 appointmentSubject = (state === null || state === void 0 || (_state$bookingConfig = state.bookingConfig) === null || _state$bookingConfig === void 0 || (_state$bookingConfig = _state$bookingConfig.config) === null || _state$bookingConfig === void 0 || (_state$bookingConfig = _state$bookingConfig.basic) === null || _state$bookingConfig === void 0 ? void 0 : _state$bookingConfig.appointment_subject) || {};
	const translation = context === null || context === void 0 || (_context$appHelper2 = context.appHelper) === null || _context$appHelper2 === void 0 || (_context$appHelper2 = _context$appHelper2.utils) === null || _context$appHelper2 === void 0 ? void 0 : _context$appHelper2.translationShop;
	const handleAdd = () => {
		setModalVisible(true);
	};
	const handleModalCancel = () => {
		var _addClientRef$current;
		setModalVisible(false);
		(_addClientRef$current = addClientRef.current) === null || _addClientRef$current === void 0 || _addClientRef$current.reset();
	};
	const renderResults = (searchResults) => {
		return /* @__PURE__ */ React.createElement(ResultList, {
			ref: resultListRef,
			dataSource: searchResults,
			hasMore,
			loading: resultLoading,
			holderTitle: (translation === null || translation === void 0 ? void 0 : translation(appointmentSubject === null || appointmentSubject === void 0 ? void 0 : appointmentSubject.form_name)) || locales.getText("pisell2.text.client-search.holder"),
			onLoadMore,
			onRowClick,
			translateText: translationOriginal
		});
	};
	const handleAddClient = (record, isChecked) => {
		onAddClient === null || onAddClient === void 0 || onAddClient(record, isChecked);
		handleModalCancel();
	};
	useImperativeHandle(ref, () => ({ resetScrollPosition: () => {
		var _resultListRef$curren;
		(_resultListRef$curren = resultListRef.current) === null || _resultListRef$curren === void 0 || _resultListRef$curren.resetScrollPosition();
	} }));
	return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(comprehensiveSearch_default, _objectSpread2(_objectSpread2({}, props), {}, {
		ref: comprehensiveSearchRef,
		renderType: "modal",
		onAdd: handleAdd,
		resultDisplay: renderResults
	})), /* @__PURE__ */ React.createElement(Add_default, {
		containerProps: {
			className: "pisell-client-search__new-client-modal",
			title: locales.getText("pisell2.text.create-new-client"),
			open: modalVisible,
			onCancel: handleModalCancel,
			width: 680
		},
		onChange: handleAddClient,
		ref: addClientRef
	}));
});
//#endregion
export { ClientSearch as default };
