"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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importStar(require("react"));
const react_redux_1 = require("react-redux");
const icons_1 = require("@ant-design/icons");
const antd_1 = require("antd");
const styled_components_1 = __importDefault(require("styled-components"));
const ACTIONS = __importStar(require("@/actions/store/list"));
const global_1 = require("@/constants/global");
const system_1 = require("@/pages/system");
const ToolRow = styled_components_1.default.div `
  display: flex;
  margin: 0 12px 12px 0;
`;
const ToolLabel = styled_components_1.default.div `
  margin: 4px 10px 4px 4px;
  line-height: 24px;
  font-weight: 500;
`;
const ToolSelect = styled_components_1.default.div `
  -webkit-box-flex: 1;
  flex-grow: 1;
`;
const SearchRow = styled_components_1.default.div `
  border-bottom: 1px solid rgb(221, 221, 221);
  line-height: 48px;
  display: flex;
  justify-content: space-between;
  margin-bottom: 18px;
`;
const SearchRowTitle = styled_components_1.default.div `
  flex: 0 0 80px;
  text-align: center;
  color: rgb(24, 144, 255);
  border-bottom: 2px solid rgb(24, 144, 255);
`;
const SearchRowContent = styled_components_1.default.div `
  flex: 0 0 300px;
  display: flex;
  justify-content: flex-end;
  align-items: center;
`;
const { Search } = antd_1.Input;
const { Option } = antd_1.Select;
const mapStateToProps = (store) => ({
    pageInfo: store.store.list.pageInfo,
    searchText: store.store.list.searchText,
    selectedAppIds: store.store.list.selectedAppIds,
    projectResourceList: store.store.list.projectResourceList,
    packageResourceList: store.store.list.packageResourceList,
    variableResourceList: store.store.list.variableResourceList,
    applicationList: store.store.list.allApplicationList,
});
const mapDispatchToProps = {
    fetchResources: ACTIONS.fetchResources,
    updateSelectedAppIds: ACTIONS.updateSelectedAppIds,
    updateSearchText: ACTIONS.updateSearchText,
    updateBuyModalVisible: ACTIONS.updateBuyModalVisible,
};
const SelectTool = (props) => {
    const { type, pageInfo, searchText, selectedAppIds, projectResourceList, packageResourceList, variableResourceList, applicationList, fetchResources, updateSelectedAppIds, updateSearchText, updateBuyModalVisible, } = props;
    const { locale } = (0, react_1.useContext)(system_1.GlobalContext);
    const { global, application, builder, store } = locale.business;
    const resourceList = type === global_1.FileType.package
        ? packageResourceList
        : type === global_1.FileType.variable
            ? variableResourceList
            : projectResourceList;
    const handleSearch = (text) => {
        fetchResources({
            type,
            appIds: selectedAppIds,
            search: text,
            page: pageInfo.page,
            size: pageInfo.size,
        });
    };
    const handleSearchTextChange = (e) => {
        updateSearchText(e.target.value);
    };
    const handleAdd = () => {
        const ids = type === global_1.FileType.page || type === global_1.FileType.template
            ? resourceList
                .filter((item) => item.checked)
                .map((item) => {
                return item.files.map((item) => item.id);
            })
                .flat()
            : resourceList.filter((item) => item.checked).map((item) => item.id);
        updateBuyModalVisible(true, ids);
    };
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement(ToolRow, null,
            react_1.default.createElement(ToolLabel, null,
                global.application,
                ":"),
            react_1.default.createElement(ToolSelect, null,
                react_1.default.createElement(antd_1.Select, { mode: "multiple", style: { width: '60%' }, placeholder: application.selectApplication, defaultValue: [], onChange: (value) => {
                        updateSelectedAppIds(value);
                        fetchResources({
                            type,
                            search: searchText,
                            page: pageInfo.page,
                            size: pageInfo.size,
                            appIds: value,
                        });
                    } }, applicationList === null || applicationList === void 0 ? void 0 : applicationList.map((application) => (react_1.default.createElement(Option, { key: application.id, value: application.id }, application.name)))))),
        react_1.default.createElement(SearchRow, null,
            react_1.default.createElement(SearchRowTitle, null,
                global.all,
                "(",
                pageInfo.total,
                ")"),
            react_1.default.createElement(SearchRowContent, null,
                react_1.default.createElement(Search, { value: searchText, placeholder: builder.componentSearch, allowClear: true, onSearch: handleSearch, onChange: handleSearchTextChange, style: { width: 200 } }),
                react_1.default.createElement(antd_1.Button, { type: "primary", icon: react_1.default.createElement(icons_1.PlusOutlined, null), style: { marginLeft: 8 }, disabled: !resourceList.find((item) => item.checked), onClick: handleAdd }, store.buy)))));
};
exports.default = (0, react_redux_1.connect)(mapStateToProps, mapDispatchToProps)(SelectTool);
