"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 index_1 = require("@/components/index");
const index_2 = require("@/constants/index");
const system_1 = require("@/pages/system");
const image_url_env_1 = require("@/utils/image-url-env");
const components_1 = require("./components");
const { TabPane } = antd_1.Tabs;
const { Meta } = antd_1.Card;
const FileName = styled_components_1.default.div `
  margin-top: 12px;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
  display: block;
`;
const PaginationWrapper = styled_components_1.default.div `
  text-align: center;
`;
const mapStateToProps = (store) => ({
    loading: store.store.list.loading,
    pageInfo: store.store.list.pageInfo,
    packageResourceList: store.store.list.packageResourceList,
    projectResourceList: store.store.list.projectResourceList,
    variableResourceList: store.store.list.variableResourceList,
    searchText: store.store.list.searchText,
    selectedAppIds: store.store.list.selectedAppIds,
    type: store.store.list.type,
});
const mapDispatchToProps = {
    clearAll: ACTIONS.clearAll,
    fetchResources: ACTIONS.fetchResources,
    fetchAllApplicationList: ACTIONS.fetchAllApplicationList,
    updateType: ACTIONS.updateType,
    updateBuyModalVisible: ACTIONS.updateBuyModalVisible,
    updatePreviewModalVisible: ACTIONS.updatePreviewModalVisible,
    updateProjectResourceItemChecked: ACTIONS.updateProjectResourceItemChecked,
    updatePackageResourceItemChecked: ACTIONS.updatePackageResourceItemChecked,
    updateVariableResourceItemChecked: ACTIONS.updateVariableResourceItemChecked,
    updateSearchText: ACTIONS.updateSearchText,
    updateSelectedAppIds: ACTIONS.updateSelectedAppIds,
};
const Store = (props) => {
    const { loading, pageInfo, packageResourceList, projectResourceList, variableResourceList, searchText, selectedAppIds, type, clearAll, fetchResources, fetchAllApplicationList, updateType, updateBuyModalVisible, updatePreviewModalVisible, updateProjectResourceItemChecked, updatePackageResourceItemChecked, updateVariableResourceItemChecked, updateSearchText, updateSelectedAppIds, } = props;
    // i18n
    const { locale } = (0, react_1.useContext)(system_1.GlobalContext);
    const { global, file, variable, store } = locale.business;
    (0, react_1.useEffect)(() => {
        // fetch page|template|package|variable list info
        fetchResources({ page: pageInfo.page, size: pageInfo.size, search: '', type: index_2.FileType.page });
        // fetch all application list
        fetchAllApplicationList({ page: 1, size: 1000 });
        return () => {
            clearAll();
        };
    }, []);
    const handleTabsChange = (data) => {
        // clear interaction data
        updateSelectedAppIds([]);
        updateSearchText('');
        updateType(data);
        fetchResources({ page: pageInfo.page, size: pageInfo.size, search: '', type: data });
    };
    const handleClickView = (e, item) => {
        e.preventDefault();
        e.stopPropagation();
        updatePreviewModalVisible(true, item);
    };
    const handleBuyClick = (e, item) => {
        e.preventDefault();
        e.stopPropagation();
        updateBuyModalVisible(true, type === index_2.FileType.package || type === index_2.FileType.variable ? [item.id] : item.files.map((item) => item.id));
    };
    const PageContent = (0, react_1.useMemo)(() => {
        const isPageOrTemplate = type === index_2.FileType.page || type === index_2.FileType.template;
        const isVariable = type === index_2.FileType.variable;
        const _type = isPageOrTemplate ? 'default' : type;
        const resourceMap = {
            variable: variableResourceList,
            package: packageResourceList,
            default: projectResourceList,
        };
        const resource = resourceMap[_type];
        return (react_1.default.createElement(antd_1.Spin, { spinning: loading },
            react_1.default.createElement(antd_1.Row, { gutter: 16, style: { minHeight: 24, paddingBottom: 24, marginLeft: 4, marginRight: 4 } },
                (resource === null || resource === void 0 ? void 0 : resource.length) > 0 &&
                    resource.map((resource) => {
                        var _a;
                        const actions = [
                            react_1.default.createElement(antd_1.Checkbox, { checked: resource.checked }),
                            react_1.default.createElement(icons_1.ShoppingCartOutlined, { onClick: (e) => {
                                    handleBuyClick(e, resource);
                                } }),
                        ];
                        if (isPageOrTemplate) {
                            actions.splice(1, 0, react_1.default.createElement(icons_1.EyeOutlined, { onClick: (e) => {
                                    handleClickView(e, resource);
                                } }));
                        }
                        return (react_1.default.createElement(antd_1.Col, { span: 6, key: resource.id },
                            react_1.default.createElement(antd_1.Card, { key: resource.id, onClick: () => {
                                    isPageOrTemplate
                                        ? updateProjectResourceItemChecked(resource.id)
                                        : isVariable
                                            ? updateVariableResourceItemChecked(resource.id)
                                            : updatePackageResourceItemChecked(resource.id);
                                }, cover: isPageOrTemplate ? (react_1.default.createElement("img", { alt: "example", src: (0, image_url_env_1.getImageUrlByEnv)('/images/placeholder.png') })) : null, actions: actions, style: { marginBottom: 12 }, hoverable: true, bordered: true },
                                react_1.default.createElement(Meta, { title: react_1.default.createElement(antd_1.Tooltip, { placement: "topLeft", title: resource.name }, resource.name), description: `${global.application}: ${(_a = resource.application) === null || _a === void 0 ? void 0 : _a.name}` }),
                                isPageOrTemplate && (react_1.default.createElement(FileName, null,
                                    file.name,
                                    ":",
                                    resource.files &&
                                        resource.files.map((item) => {
                                            return (react_1.default.createElement("span", { key: item.id, style: { marginLeft: 6 } }, item.name));
                                        }))))));
                    }),
                !loading && (resource === null || resource === void 0 ? void 0 : resource.length) === 0 && (react_1.default.createElement(antd_1.Empty, { image: antd_1.Empty.PRESENTED_IMAGE_SIMPLE, style: { margin: '48px auto' } })))));
    }, [loading, type, projectResourceList, packageResourceList, variableResourceList, global, file]);
    const PaneContent = (0, react_1.useMemo)(() => {
        return (react_1.default.createElement(react_1.default.Fragment, null,
            react_1.default.createElement(components_1.SelectTool, { type: type }),
            PageContent,
            react_1.default.createElement(PaginationWrapper, null,
                react_1.default.createElement(antd_1.Pagination, { showQuickJumper: true, hideOnSinglePage: true, current: pageInfo.page, pageSize: pageInfo.size, total: pageInfo.total, onChange: (page) => {
                        fetchResources({
                            page,
                            size: pageInfo.size,
                            search: searchText,
                            appIds: selectedAppIds,
                            type,
                        });
                    } }))));
    }, [type, PageContent]);
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement(index_1.Content, null,
            react_1.default.createElement(index_1.FoxPageContent, { breadcrumb: react_1.default.createElement(index_1.FoxPageBreadcrumb, { breadCrumb: [
                        {
                            name: store.name,
                        },
                    ] }), style: { maxWidth: index_2.WIDTH_DEFAULT, margin: '0 auto', overflow: 'unset' } },
                react_1.default.createElement(antd_1.Tabs, { centered: true, size: "large", destroyInactiveTabPane: true, defaultActiveKey: "page", onChange: handleTabsChange },
                    react_1.default.createElement(TabPane, { tab: file.page, key: index_2.FileType.page }, PaneContent),
                    react_1.default.createElement(TabPane, { tab: file.template, key: index_2.FileType.template }, PaneContent),
                    react_1.default.createElement(TabPane, { tab: file.package, key: index_2.FileType.package }, PaneContent),
                    react_1.default.createElement(TabPane, { tab: variable.title, key: index_2.FileType.variable }, PaneContent)))),
        react_1.default.createElement(components_1.BuyModal, null),
        react_1.default.createElement(components_1.PreviewModal, null)));
};
exports.default = (0, react_redux_1.connect)(mapStateToProps, mapDispatchToProps)(Store);
