import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _asyncToGenerator } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import useEngineContext from "../../../hooks/useEngineContext.js";
import { getFormConfig, getHolderList } from "../serve.js";
import React, { useState } from "react";
import { Button, Col, Row } from "antd";
import { isMobile, locales } from "@pisell/utils";
import { Icon, PisellAvatar, PisellModal, Spin as Spin$1 } from "@pisell/materials";
import { useRequest } from "ahooks";
import "./index.less";
//#region src/components/walletList/tempHolderSelector/index.tsx
const ACol = Col;
const ARow = 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] = useState(props.value);
	const { visible, onCancel, onConfirm, confirmLoading } = props;
	const context = useEngineContext();
	const _getHolderList = useRequest(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 = _asyncToGenerator(function* () {
			var _formConfig$form, _context$appHelper, _context$appHelper$ac;
			let _formForFilling = props.formId;
			const { data: formConfig } = yield 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: 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) => _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.createElement(PisellModal, {
		title: locales.getText("page.wallet.tempHolderSelector.title"),
		platform: isMobile() ? "h5" : "pc",
		visible,
		onClose: onCancel,
		onOk: () => {},
		onCancel,
		footer: /* @__PURE__ */ React.createElement(Button, {
			type: "primary",
			block: true,
			onClick: () => {
				onConfirm(value);
			},
			disabled: !value,
			loading: confirmLoading
		}, locales.getText("page.wallet.tempHolderSelector.confirm")),
		width: 640,
		bodyStyle: { padding: 0 },
		headerDivider: false,
		destroyOnClose: true,
		className: "temp-holder-selector-modal"
	}, /* @__PURE__ */ React.createElement(Spin$1, { spinning: _getHolderList.loading }, /* @__PURE__ */ React.createElement("div", { className: "temp-holder-selector-content" }, /* @__PURE__ */ React.createElement(ARow, { gutter: [16, 16] }, /* @__PURE__ */ React.createElement(ACol, {
		span: 8,
		xs: { span: 12 },
		sm: { span: 8 }
	}, /* @__PURE__ */ React.createElement("div", { className: "temp-holder-selector-block-wrapper" }, /* @__PURE__ */ React.createElement("div", {
		className: "temp-holder-selector-block temp-holder-selector-block-create",
		onClick: onAddHolder
	}, /* @__PURE__ */ React.createElement("div", { className: "temp-holder-selector-block-avatar" }, /* @__PURE__ */ React.createElement(PisellAvatar, {
		useTextAvatar: true,
		size: "xxl"
	}, /* @__PURE__ */ React.createElement(Icon, {
		type: "pisell2-plus",
		style: { fontSize: "24px" }
	}))), /* @__PURE__ */ React.createElement("div", { className: "temp-holder-selector-block-name" }, 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.createElement(ACol, {
		span: 8,
		xs: { span: 12 },
		sm: { span: 8 },
		key: item.id
	}, /* @__PURE__ */ React.createElement("div", { className: `temp-holder-selector-block-wrapper ${value === item.form_record_id ? "temp-holder-selector-block-wrapper-active" : ""}` }, /* @__PURE__ */ React.createElement("div", {
		className: "temp-holder-selector-block",
		onClick: () => {
			setValue(item.form_record_id);
		}
	}, /* @__PURE__ */ React.createElement("div", { className: "temp-holder-selector-block-avatar" }, /* @__PURE__ */ React.createElement(PisellAvatar, {
		useTextAvatar: true,
		size: "xxl"
	}, getAvatarText(item.main_field))), /* @__PURE__ */ React.createElement("div", { className: "temp-holder-selector-block-name" }, item.main_field)))))))));
}
//#endregion
export { TempHolderSelector as default };
