const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_asyncToGenerator = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js");
const require_useEngineContext = require("../../../hooks/useEngineContext.js");
const require_serve = require("../serve.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
let ahooks = require("ahooks");
require("./index.less");
//#region src/components/walletList/tempHolderSelector/index.tsx
const ACol = antd.Col;
const ARow = antd.Row;
const getAvatarText = (name) => {
	if (!name) return "";
	return name.trim().split(/\s+/).slice(0, 2).map((word) => word.charAt(0).toUpperCase()).join("");
};
function TempHolderSelector(props) {
	var _getHolderList$data;
	const [value, setValue] = (0, react.useState)(props.value);
	const { visible, onCancel, onConfirm, confirmLoading } = props;
	const context = require_useEngineContext.default();
	const _getHolderList = (0, ahooks.useRequest)(require_serve.getHolderList, {
		defaultParams: [{
			skip: 1,
			num: 100,
			form_id: props.formId,
			customer_id: props.customerId
		}],
		onSuccess: (res) => {
			if (Array.isArray(res.list) && res.list.length > 0) setValue(res.list[0].form_record_id);
		}
	});
	const onAddHolder = function() {
		var _ref = require_asyncToGenerator._asyncToGenerator(function* () {
			var _formConfig$form, _context$appHelper, _context$appHelper$ac;
			let _formForFilling = props.formId;
			const { data: formConfig } = yield require_serve.getFormConfig(props.formId);
			if ((formConfig === null || formConfig === void 0 || (_formConfig$form = formConfig.form) === null || _formConfig$form === void 0 ? void 0 : _formConfig$form.form_type) === "minors" && Array.isArray(formConfig === null || formConfig === void 0 ? void 0 : formConfig.field)) {
				var _formConfig$field;
				const detail = formConfig === null || formConfig === void 0 || (_formConfig$field = formConfig.field) === null || _formConfig$field === void 0 ? void 0 : _formConfig$field.find((item) => item.filed_type === "form_to_form" && item.status === "hide");
				if (detail) {
					var _detail$setting;
					_formForFilling = detail === null || detail === void 0 || (_detail$setting = detail.setting) === null || _detail$setting === void 0 ? void 0 : _detail$setting.linkedTable;
				}
			}
			context === null || context === void 0 || (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 || (_context$appHelper = _context$appHelper.utils) === null || _context$appHelper === void 0 || (_context$appHelper$ac = _context$appHelper.action) === null || _context$appHelper$ac === void 0 || _context$appHelper$ac.call(_context$appHelper, {
				type: "pisell1.form",
				data: {
					form_id: _formForFilling,
					form_title: _pisell_utils.locales.getText("page.wallet.tempHolderSelector.create"),
					isV2API: true
				},
				callback: (res) => {
					if ((res === null || res === void 0 ? void 0 : res.status) === "success") if (_formForFilling !== props.formId) _getHolderList.refresh();
					else {
						_getHolderList.mutate((prev) => require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, prev), {}, { list: [res.values, ...prev.list] }));
						setValue(res.values.form_record_id);
					}
				}
			});
		});
		return function onAddHolder() {
			return _ref.apply(this, arguments);
		};
	}();
	return /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellModal, {
		title: _pisell_utils.locales.getText("page.wallet.tempHolderSelector.title"),
		platform: (0, _pisell_utils.isMobile)() ? "h5" : "pc",
		visible,
		onClose: onCancel,
		onOk: () => {},
		onCancel,
		footer: /* @__PURE__ */ react.default.createElement(antd.Button, {
			type: "primary",
			block: true,
			onClick: () => {
				onConfirm(value);
			},
			disabled: !value,
			loading: confirmLoading
		}, _pisell_utils.locales.getText("page.wallet.tempHolderSelector.confirm")),
		width: 640,
		bodyStyle: { padding: 0 },
		headerDivider: false,
		destroyOnClose: true,
		className: "temp-holder-selector-modal"
	}, /* @__PURE__ */ react.default.createElement(_pisell_materials.Spin, { spinning: _getHolderList.loading }, /* @__PURE__ */ react.default.createElement("div", { className: "temp-holder-selector-content" }, /* @__PURE__ */ react.default.createElement(ARow, { gutter: [16, 16] }, /* @__PURE__ */ react.default.createElement(ACol, {
		span: 8,
		xs: { span: 12 },
		sm: { span: 8 }
	}, /* @__PURE__ */ react.default.createElement("div", { className: "temp-holder-selector-block-wrapper" }, /* @__PURE__ */ react.default.createElement("div", {
		className: "temp-holder-selector-block temp-holder-selector-block-create",
		onClick: onAddHolder
	}, /* @__PURE__ */ react.default.createElement("div", { className: "temp-holder-selector-block-avatar" }, /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellAvatar, {
		useTextAvatar: true,
		size: "xxl"
	}, /* @__PURE__ */ react.default.createElement(_pisell_materials.Icon, {
		type: "pisell2-plus",
		style: { fontSize: "24px" }
	}))), /* @__PURE__ */ react.default.createElement("div", { className: "temp-holder-selector-block-name" }, _pisell_utils.locales.getText("page.wallet.tempHolderSelector.create"))))), Array.isArray((_getHolderList$data = _getHolderList.data) === null || _getHolderList$data === void 0 ? void 0 : _getHolderList$data.list) && _getHolderList.data.list.map((item) => /* @__PURE__ */ react.default.createElement(ACol, {
		span: 8,
		xs: { span: 12 },
		sm: { span: 8 },
		key: item.id
	}, /* @__PURE__ */ react.default.createElement("div", { className: `temp-holder-selector-block-wrapper ${value === item.form_record_id ? "temp-holder-selector-block-wrapper-active" : ""}` }, /* @__PURE__ */ react.default.createElement("div", {
		className: "temp-holder-selector-block",
		onClick: () => {
			setValue(item.form_record_id);
		}
	}, /* @__PURE__ */ react.default.createElement("div", { className: "temp-holder-selector-block-avatar" }, /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellAvatar, {
		useTextAvatar: true,
		size: "xxl"
	}, getAvatarText(item.main_field))), /* @__PURE__ */ react.default.createElement("div", { className: "temp-holder-selector-block-name" }, item.main_field)))))))));
}
//#endregion
exports.default = TempHolderSelector;
