import { _asyncToGenerator } from "../../../../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/asyncToGenerator.js";
import { getMachineCodeList } from "../serve.js";
import { formatMachineCodeServer2CardList, getDataByIds } from "../utils.js";
import React, { forwardRef, memo, useEffect, useImperativeHandle, useMemo, useState } from "react";
import { isFunction, locales } from "@pisell/utils";
import { Iconfont, PisellCardList, PisellEmpty, PisellLoading } from "@pisell/materials";
import { useMemoizedFn } from "ahooks";
import "./index.less";
var List_default = memo(forwardRef((props, ref) => {
	const { title, onChange, data: propsData, value, walletSetting, onLoadingChange, loading: propsLoading, onChangeDebounce } = props;
	const [data, setData] = useState(propsData || []);
	const [loading, setLoading] = useState(false);
	useEffect(() => {
		if (propsData) setData(propsData);
	}, [propsData]);
	useEffect(() => {
		setLoading(!!propsLoading);
	}, [propsLoading]);
	const getList = function() {
		var _ref = _asyncToGenerator(function* (params, formatList) {
			setLoading(true);
			try {
				let list = (yield getMachineCodeList(params)) || [];
				if (isFunction(formatList)) list = formatList(list);
				setData(formatMachineCodeServer2CardList(list, value));
			} catch (err) {
				console.error(err);
			}
			setLoading(false);
		});
		return function getList(_x, _x2) {
			return _ref.apply(this, arguments);
		};
	}();
	const init = (params, formatList) => {
		getList(params, formatList);
	};
	useImperativeHandle(ref, () => ({ init }));
	useEffect(() => {
		onLoadingChange === null || onLoadingChange === void 0 || onLoadingChange(loading);
	}, [loading]);
	/**
	* 拆分出可用的和不可用的列表
	*/
	const { availableList, disabledList } = useMemo(() => {
		const _disabledList = [];
		const _availableList = [];
		data.forEach((item) => {
			if (item.disabled) _disabledList.push(item);
			else _availableList.push(item);
		});
		return {
			disabledList: _disabledList,
			availableList: _availableList
		};
	}, [data]);
	const handleChange = useMemoizedFn((val) => {
		onChange === null || onChange === void 0 || onChange(val, getDataByIds(data, val));
	});
	const empty = useMemo(() => {
		return /* @__PURE__ */ React.createElement(PisellEmpty, {
			icon: /* @__PURE__ */ React.createElement(Iconfont, {
				type: "pisell2-a-wallet-021",
				style: { color: "#667085" }
			}),
			description: locales.getText("tocPay.text.walletPassEmpty")(walletSetting === null || walletSetting === void 0 ? void 0 : walletSetting.name)
		});
	}, []);
	return /* @__PURE__ */ React.createElement(PisellLoading, { loading }, /* @__PURE__ */ React.createElement("div", { className: "pisell-walletPass-list-wrap" }, title && /* @__PURE__ */ React.createElement("div", { className: "pisell-walletPass-list-title" }, title), /* @__PURE__ */ React.createElement(PisellCardList, {
		data: availableList,
		hasMore: false,
		value,
		onChange: handleChange,
		showEmpty: data.length === 0,
		empty,
		onChangeDebounce
	}), !!disabledList.length && /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("div", { className: "pisell-walletPass-list-disabled" }, locales.getText("tocPay.text.walletPassDisabled")), /* @__PURE__ */ React.createElement(PisellCardList, {
		mode: void 0,
		data: disabledList,
		hasMore: false
	}))));
}));
//#endregion
export { List_default as default };
