"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importStar(require("react"));
const react_redux_1 = require("react-redux");
const antd_1 = require("antd");
const ACTIONS = __importStar(require("@/actions/store/list"));
const index_1 = require("@/constants/index");
const system_1 = require("@/pages/system");
const { Option } = antd_1.Select;
const PAGE_NUMBER = 1;
const PAGE_SIZE = 9999;
const mapStateToProps = (store) => ({
    organizationId: store.system.user.organizationId,
    buyModalVisible: store.store.list.buyModalVisible,
    applicationList: store.store.list.applicationList,
    buyIds: store.store.list.buyIds,
    type: store.store.list.type,
});
const mapDispatchToProps = {
    updateBuyModalVisible: ACTIONS.updateBuyModalVisible,
    addGoods: ACTIONS.addGoods,
    fetchApplicationList: ACTIONS.fetchApplicationList,
};
const BuyModal = (props) => {
    const { organizationId, type, buyModalVisible, applicationList, buyIds, updateBuyModalVisible, fetchApplicationList, addGoods, } = props;
    const [selectedAppIds, setSelectedAppIds] = (0, react_1.useState)([]);
    const [deliveryType, setDeliveryType] = (0, react_1.useState)();
    const { locale } = (0, react_1.useContext)(system_1.GlobalContext);
    const { application, store } = locale.business;
    const deliveryTypeMap = [
        {
            label: store.refer,
            value: index_1.StoreGoodsPurchaseType.reference,
        },
        {
            label: store.clone,
            value: index_1.StoreGoodsPurchaseType.clone,
        },
    ];
    (0, react_1.useEffect)(() => {
        if (!buyModalVisible) {
            setSelectedAppIds([]);
            setDeliveryType(undefined);
        }
    }, [buyModalVisible]);
    (0, react_1.useEffect)(() => {
        if (buyModalVisible) {
            fetchApplicationList({ organizationId, page: PAGE_NUMBER, size: PAGE_SIZE });
        }
    }, [buyModalVisible, fetchApplicationList, organizationId]);
    const handleClose = () => {
        updateBuyModalVisible(false);
    };
    const handleOnOk = () => {
        addGoods({
            appIds: selectedAppIds,
            goodsIds: buyIds,
            delivery: type === index_1.FileType.package
                ? deliveryType || index_1.StoreGoodsPurchaseType.reference
                : index_1.StoreGoodsPurchaseType.clone,
        });
    };
    return (react_1.default.createElement(antd_1.Modal, { title: store.buyModalTitle, destroyOnClose: true, maskClosable: false, visible: buyModalVisible, onCancel: handleClose, onOk: handleOnOk },
        react_1.default.createElement(antd_1.Select, { mode: "multiple", placeholder: application.selectApplication, value: selectedAppIds, onChange: setSelectedAppIds, style: { width: '100%' } }, applicationList === null || applicationList === void 0 ? void 0 : applicationList.map((application) => (react_1.default.createElement(Option, { key: application.id, value: application.id }, application.name)))),
        type === 'package' && (react_1.default.createElement(antd_1.Select, { showArrow: false, placeholder: store.deliveryTypePlaceholder, options: deliveryTypeMap, value: deliveryType, onChange: setDeliveryType, style: { width: '100%', marginTop: 12 } }))));
};
exports.default = (0, react_redux_1.connect)(mapStateToProps, mapDispatchToProps)(BuyModal);
