"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 react_router_dom_1 = require("react-router-dom");
const icons_1 = require("@ant-design/icons");
const antd_1 = require("antd");
const ACTIONS = __importStar(require("@/actions/applications/detail/packages/detail"));
const FAST_ACTIONS = __importStar(require("@/actions/applications/detail/packages/fast"));
const RESOURCES_ACTIONS = __importStar(require("@/actions/applications/detail/resources/groups"));
const index_1 = require("@/components/index");
const file_1 = require("@/constants/file");
const system_1 = require("@/pages/system");
const location_info_1 = require("@/utils/location-info");
const ComponentResource_1 = __importDefault(require("./components/ComponentResource"));
require("./style.css");
const { Option } = antd_1.Select;
const mapStateToProps = (store) => {
    var _a, _b;
    return ({
        applicationId: store.applications.detail.settings.app.applicationId,
        groupList: store.applications.detail.resources.groups.groupList,
        open: store.applications.detail.packages.detail.cloudSyncDrawer.open,
        loading: store.applications.detail.packages.detail.cloudSyncDrawer.loading,
        componentInfo: store.applications.detail.packages.detail.componentInfo,
        fileDetail: store.applications.detail.packages.detail.fileDetail,
        groupLoading: store.applications.detail.packages.fast.loading,
        saving: store.applications.detail.packages.fast.saving,
        lastVersion: (_a = store.applications.detail.packages.fast.packages[0]) === null || _a === void 0 ? void 0 : _a.lastVersion,
        componentRemote: (_b = store.applications.detail.packages.fast.packages[0]) === null || _b === void 0 ? void 0 : _b.components[0],
    });
};
const mapDispatchToProps = {
    closeDrawer: ACTIONS.updateCloudSyncDrawerOpenStatus,
    fetchFileDetail: ACTIONS.fetchFileDetail,
    fetchComponentVersionsAction: ACTIONS.fetchComponentVersionsAction,
    fetchComponentRemotes: FAST_ACTIONS.fetchPackages,
    update: FAST_ACTIONS.updateChanges,
    save: FAST_ACTIONS.saveChanges,
    selectGroup: FAST_ACTIONS.selectGroup,
    select: FAST_ACTIONS.updateSelected,
    fetchGroups: RESOURCES_ACTIONS.fetchResourcesGroups,
};
const ComponentCloudSyncDrawer = (props) => {
    const { applicationId, open, loading, groupLoading, groupList, componentInfo, componentRemote, fileDetail, lastVersion, fetchGroups, fetchComponentRemotes, closeDrawer, fetchFileDetail, update, save, selectGroup, saving, select, fetchComponentVersionsAction, } = props;
    const [groupId, setGroupId] = (0, react_1.useState)('');
    // url params
    const { fileId } = (0, location_info_1.getLocationIfo)((0, react_router_dom_1.useLocation)());
    // i18n
    const { locale } = (0, react_1.useContext)(system_1.GlobalContext);
    const { global, version: versionI18n, resource } = locale.business;
    (0, react_1.useEffect)(() => {
        if (open && applicationId) {
            fetchGroups({
                applicationId,
            });
            if (fileId)
                fetchFileDetail({ applicationId, ids: [fileId] });
        }
    }, [open, applicationId]);
    (0, react_1.useEffect)(() => {
        if (componentRemote) {
            const name = componentRemote.resource.name;
            select([name]);
        }
    }, [componentRemote]);
    (0, react_1.useEffect)(() => {
        var _a, _b;
        const newGroupId = ((_b = (_a = fileDetail === null || fileDetail === void 0 ? void 0 : fileDetail.tags) === null || _a === void 0 ? void 0 : _a.find((item) => item.type === file_1.ResourceTagEnum.ResourceGroup && item.resourceGroupId)) === null || _b === void 0 ? void 0 : _b.resourceGroupId) || '';
        setGroupId(newGroupId);
        if (newGroupId && open) {
            handleSearch(newGroupId);
        }
    }, [fileDetail === null || fileDetail === void 0 ? void 0 : fileDetail.tags, open]);
    const handleSave = () => {
        if (applicationId)
            save(applicationId, () => {
                closeDrawer(false);
                fetchComponentVersionsAction({});
            });
    };
    const handleSearch = (groupId) => {
        if (!groupId) {
            antd_1.message.warning(resource.groupError);
            return;
        }
        selectGroup(groupId);
        if (applicationId)
            fetchComponentRemotes({
                applicationId,
                groupId,
                name: componentInfo.title,
            });
    };
    return (react_1.default.createElement(index_1.OperationDrawer, { open: open, title: versionI18n.syncTitle, onClose: () => closeDrawer(false), width: 700, destroyOnClose: true, actions: react_1.default.createElement(antd_1.Button, { type: "primary", loading: saving, onClick: handleSave }, global.apply) },
        react_1.default.createElement(react_1.default.Fragment, null,
            react_1.default.createElement(antd_1.Spin, { spinning: groupLoading || saving },
                react_1.default.createElement(index_1.Group, null,
                    react_1.default.createElement(antd_1.Row, null,
                        react_1.default.createElement(antd_1.Col, { span: 2, offset: 2, style: { paddingTop: 5 } },
                            react_1.default.createElement(index_1.Title, null,
                                resource.group,
                                ":")),
                        react_1.default.createElement(antd_1.Col, { span: 12 },
                            react_1.default.createElement(antd_1.Select, { placeholder: resource.group, value: groupId, style: { width: '100%' }, onChange: (value) => {
                                    setGroupId(value);
                                } }, groupList === null || groupList === void 0 ? void 0 : groupList.map((item) => (react_1.default.createElement(Option, { key: item.id, value: item.id }, item.name))))),
                        react_1.default.createElement(antd_1.Col, { span: 6 },
                            react_1.default.createElement(antd_1.Button, { type: "default", onClick: () => {
                                    handleSearch(groupId);
                                }, style: { marginLeft: 12 } },
                                react_1.default.createElement(icons_1.SearchOutlined, null),
                                " ",
                                global.search))))),
            react_1.default.createElement(antd_1.Spin, { spinning: loading, style: { minHeight: 100 } },
                react_1.default.createElement(ComponentResource_1.default, { lastVersion: lastVersion, componentRemote: componentRemote, onChange: (value) => update((componentRemote === null || componentRemote === void 0 ? void 0 : componentRemote.resource.name) || '', value) })))));
};
exports.default = (0, react_redux_1.connect)(mapStateToProps, mapDispatchToProps)(ComponentCloudSyncDrawer);
