import { i as __read, b as __awaiter, c as __generator, y as IconButton, aq as Button, ay as apiDataPkgFunc, a8 as get, t as toastApi } from './MapEditView-24782753.js';
import { observer } from 'mobx-react';
import React__default, { useCallback } from 'react';
import { t as thumbListContext, A as ActionTypeEnum, a as i18n, b as prefixCls } from './index-3505e026.js';
import 'react-dom';
import 'react-dnd-html5-backend';
import 'classnames';
import 'perfect-scrollbar';
import 'react-virtualized';
import 'maptalks';
import 'lodash';
import 'uuid';
import 'mobx';
import 'lz-string';
import 'dayjs';
import 'react-is';
import '@loadable/component';
import 'js-beautify';
import 'axios';
import 'superstruct';
import '@tanem/react-nprogress';
import 'jquery';
import 'chroma-js';
import 'js-cookie';
import 'is-email';
import 'is-uuid';
import 'is-url';
import 'anymatch';
import 'perf-request';
import '@syncpoint/wkx';
import 'buffer';
import '@turf/turf';
import '@mapbox/polylabel';
import 'turf-multipolygon';
import 'turf-polygon';
import 'gcoord';
import 'eventemitter2';
import 'geobuf';
import 'localforage';
import 'rc-menu';
import 'ahooks';
import 'jszip';
import 'file-saver';
import 'screenfull';
import 'sql-formatter-fork';
import 'array-move';
import 'turf-multilinestring';
import 'copy-to-clipboard';
import 'maptalks.three';
import 'ajv';
import 'spark-md5';
import 'react-draggable';
import 'turf-point';
import 'decamelize';
import 'rbush';
import 'qs';
import 'maptalks.snap';
import 'react-use';
import 'react-rnd';
import 'react-beautiful-dnd';
import 'react-contextmenu';
import 'coordtransform';
import './compact-6f8b3541.js';
import './useDataValue-8bdb351c.js';
import 'ali-react-table-fork';
import './Transfer-f55a5877.js';
import 'ismobilejs';

var eventEmit = window.__DM_GLOBE_UTIL.getEmitter();
var CollaborationHeader = observer(function (_a) {
    var onClose = _a.onClose;
    var Context = thumbListContext.getContext();
    var value = React__default.useContext(Context);
    var store = value.store;
    var stepIndex = store.stepIndex, changeStepIndex = store.changeStepIndex, allConditionStoreHasVal = store.allConditionStoreHasVal, actionType = store.actionType, generateConditionInfo = store.generateConditionInfo, packageItem = store.packageItem, leastOneChanged = store.leastOneChanged, willDeleteCollaborateInfo = store.willDeleteCollaborateInfo, onClearWillDeleteCollaborateInfo = store.onClearWillDeleteCollaborateInfo;
    var isEdit = actionType === ActionTypeEnum.edit;
    var isView = actionType === ActionTypeEnum.view;
    var confirmTextArrs = [i18n.collaboration.nextStep, i18n.collaboration.startCollaboration];
    var cancelTextSecondStep = isEdit
        ? i18n.collaboration.setCollaboration
        : i18n.collaboration.prevStep;
    var cancelTextArrs = [i18n.collaboration.cancel, cancelTextSecondStep];
    var titleArrs = [
        i18n.collaboration.startCollaborationPackage,
        i18n.collaboration.collaborationPackageDetail,
    ];
    // useMount(() => {
    //   setTimeout(() => {
    //     const info = {
    //       packageUuid: 'd28c4d86-f202-47de-ad7b-460d93c2c23f',
    //       [`update_time`]: 1634194504,
    //       [`user_id`]: 543519,
    //       [`create_time`]: 1634192352,
    //       [`package_type`]: 'collaboration_v2',
    //     };
    //     sessionStorage.setItem('collaboratePackageV2Info', JSON.stringify(info));
    //     eventEmit.emit('CHANGE_PACKAGE');
    //   }, 1000);
    // });
    var _b = __read(React__default.useState(false), 2), loading = _b[0], setLoading = _b[1];
    var onCancel = useCallback(function () {
        changeStepIndex('prev', onClose);
    }, [changeStepIndex, onClose]);
    //删除协同数据包之前的用户，例如账号a以前有查看并编辑的权限，现在a没权限了，就需要删除
    var deleteUserIds = useCallback(function () { return __awaiter(void 0, void 0, void 0, function () {
        var res;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    console.log('willDeleteCollaborateInfo', JSON.stringify(willDeleteCollaborateInfo));
                    if (!(willDeleteCollaborateInfo.length !== 0)) return [3 /*break*/, 2];
                    return [4 /*yield*/, apiDataPkgFunc().deleteDatapkgCollaborate({
                            datapkgId: packageItem.packageUuid,
                            datapkgCollaboratePost: {
                                collaborate_info: willDeleteCollaborateInfo,
                            },
                        })];
                case 1:
                    res = _a.sent();
                    console.log('🚀 ~ file: CollaborationStore.tsx ~ line 921 ~ CollaborationStore ~ res ~ res', res);
                    _a.label = 2;
                case 2: return [2 /*return*/];
            }
        });
    }); }, [packageItem.packageUuid, willDeleteCollaborateInfo]);
    // 发起协同
    var changeCollaborate = useCallback(function () { return __awaiter(void 0, void 0, void 0, function () {
        var collaborateInfo, tip, res, requestParameters, error_1, msg;
        var _a;
        var _b, _c, _d;
        return __generator(this, function (_e) {
            switch (_e.label) {
                case 0:
                    setLoading(true);
                    _e.label = 1;
                case 1:
                    _e.trys.push([1, 7, , 8]);
                    return [4 /*yield*/, deleteUserIds()];
                case 2:
                    _e.sent();
                    onClearWillDeleteCollaborateInfo();
                    collaborateInfo = generateConditionInfo();
                    tip = isEdit
                        ? i18n.collaboration.updateCollaborationData
                        : i18n.collaboration.createCollaborationData;
                    res = null;
                    requestParameters = {
                        datapkgId: packageItem.packageUuid,
                        datapkgCollaboratePost: (_a = {},
                            _a["collaborate_info"] = collaborateInfo,
                            _a),
                    };
                    if (!isEdit) return [3 /*break*/, 4];
                    return [4 /*yield*/, apiDataPkgFunc().putDatapkgCollaborate(requestParameters)];
                case 3:
                    res = _e.sent();
                    return [3 /*break*/, 6];
                case 4: return [4 /*yield*/, apiDataPkgFunc().postDatapkgCollaborate(requestParameters)];
                case 5:
                    res = _e.sent();
                    _e.label = 6;
                case 6:
                    if (get(res, 'data.rc') === 0) {
                        onClose();
                        toastApi.success(tip({ name: packageItem.name }));
                        sessionStorage.setItem('collaboratePackageV2Info', JSON.stringify({
                            packageUuid: packageItem.packageUuid,
                            createTime: get(res, 'data.result.create_time'),
                            updateTime: get(res, 'data.result.update_time'),
                            userId: get(res, 'data.result.user_id'),
                            extra: {
                                packageType: get(res, 'data.result.package_type'),
                                datasetId: (_b = packageItem.extra) === null || _b === void 0 ? void 0 : _b.datasetId,
                            },
                        }));
                        eventEmit.emit('CHANGE_PACKAGE');
                    }
                    else {
                        toastApi.error((_c = get(res, 'data.message')) !== null && _c !== void 0 ? _c : '保存失败');
                    }
                    setLoading(false);
                    return [3 /*break*/, 8];
                case 7:
                    error_1 = _e.sent();
                    console.log('error', error_1);
                    setLoading(false);
                    msg = (_d = get(error_1, 'response.message')) !== null && _d !== void 0 ? _d : get(error_1, 'response.data.message');
                    toastApi.error(msg);
                    return [3 /*break*/, 8];
                case 8: return [2 /*return*/];
            }
        });
    }); }, [
        deleteUserIds,
        onClearWillDeleteCollaborateInfo,
        generateConditionInfo,
        isEdit,
        packageItem.packageUuid,
        packageItem.name,
        packageItem.extra,
        onClose,
    ]);
    var onConfirm = useCallback(function () {
        changeStepIndex('next');
        if (stepIndex === 1) {
            changeCollaborate();
        }
    }, [changeStepIndex, changeCollaborate, stepIndex]);
    return (React__default.createElement("div", { className: "".concat(prefixCls, "-header") },
        React__default.createElement("div", { className: "".concat(prefixCls, "-header-left") },
            React__default.createElement(IconButton, { icon: "close", type: "only-icon", className: "".concat(prefixCls, "-close"), onClick: onClose }),
            titleArrs[stepIndex]),
        !isView && (React__default.createElement("div", { className: "".concat(prefixCls, "-header-right") },
            React__default.createElement(Button, { type: "assist-bg", onClick: onCancel }, cancelTextArrs[stepIndex]),
            React__default.createElement(Button, { type: "primary", onClick: onConfirm, disabled: !allConditionStoreHasVal || !leastOneChanged, loading: loading }, confirmTextArrs[stepIndex])))));
});

export { CollaborationHeader as default };
