import { a as __awaiter, s as IconButton, al as Button, at as apiDataPkgFunc, a3 as get, t as toastApi } from './MapEditView-f6381866.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-0b8bebb4.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 '@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-f89fbdd0.js';
import './useDataValue-6bd3790f.js';
import 'ali-react-table-fork';
import './Transfer-b219601e.js';
import 'ismobilejs';

const eventEmit = window.__DM_GLOBE_UTIL.getEmitter();
const CollaborationHeader = observer(({ onClose }) => {
    const Context = thumbListContext.getContext();
    const value = React__default.useContext(Context);
    const { store } = value;
    const { stepIndex, changeStepIndex, allConditionStoreHasVal, actionType, generateConditionInfo, packageItem, leastOneChanged, willDeleteCollaborateInfo, onClearWillDeleteCollaborateInfo, } = store;
    const isEdit = actionType === ActionTypeEnum.edit;
    const isView = actionType === ActionTypeEnum.view;
    const confirmTextArrs = [i18n.collaboration.nextStep, i18n.collaboration.startCollaboration];
    const cancelTextSecondStep = isEdit
        ? i18n.collaboration.setCollaboration
        : i18n.collaboration.prevStep;
    const cancelTextArrs = [i18n.collaboration.cancel, cancelTextSecondStep];
    const 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);
    // });
    const [loading, setLoading] = React__default.useState(false);
    const onCancel = useCallback(() => {
        changeStepIndex('prev', onClose);
    }, [changeStepIndex, onClose]);
    //删除协同数据包之前的用户，例如账号a以前有查看并编辑的权限，现在a没权限了，就需要删除
    const deleteUserIds = useCallback(() => __awaiter(void 0, void 0, void 0, function* () {
        console.log('willDeleteCollaborateInfo', JSON.stringify(willDeleteCollaborateInfo));
        if (willDeleteCollaborateInfo.length !== 0) {
            const res = yield apiDataPkgFunc().deleteDatapkgCollaborate({
                datapkgId: packageItem.packageUuid,
                datapkgCollaboratePost: {
                    collaborate_info: willDeleteCollaborateInfo,
                },
            });
            console.log('🚀 ~ file: CollaborationStore.tsx ~ line 921 ~ CollaborationStore ~ res ~ res', res);
        }
    }), [packageItem.packageUuid, willDeleteCollaborateInfo]);
    // 发起协同
    const changeCollaborate = useCallback(() => __awaiter(void 0, void 0, void 0, function* () {
        var _a, _b, _c;
        setLoading(true);
        try {
            yield deleteUserIds();
            onClearWillDeleteCollaborateInfo();
            const collaborateInfo = generateConditionInfo();
            // const requestApi = isEdit
            //   ? apiDataPkgFunc().putDatapkgCollaborate
            //   : apiDataPkgFunc().postDatapkgCollaborate;
            const tip = isEdit
                ? i18n.collaboration.updateCollaborationData
                : i18n.collaboration.createCollaborationData;
            let res = null;
            const requestParameters = {
                datapkgId: packageItem.packageUuid,
                datapkgCollaboratePost: {
                    [`collaborate_info`]: collaborateInfo,
                },
            };
            if (isEdit) {
                res = yield apiDataPkgFunc().putDatapkgCollaborate(requestParameters);
            }
            else {
                res = yield apiDataPkgFunc().postDatapkgCollaborate(requestParameters);
            }
            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: (_a = packageItem.extra) === null || _a === void 0 ? void 0 : _a.datasetId,
                    },
                }));
                eventEmit.emit('CHANGE_PACKAGE');
            }
            else {
                toastApi.error((_b = get(res, 'data.message')) !== null && _b !== void 0 ? _b : '保存失败');
            }
            setLoading(false);
        }
        catch (error) {
            console.log('error', error);
            setLoading(false);
            const msg = (_c = get(error, 'response.message')) !== null && _c !== void 0 ? _c : get(error, 'response.data.message');
            toastApi.error(msg);
        }
    }), [
        deleteUserIds,
        onClearWillDeleteCollaborateInfo,
        generateConditionInfo,
        isEdit,
        packageItem.packageUuid,
        packageItem.name,
        packageItem.extra,
        onClose,
    ]);
    const onConfirm = useCallback(() => {
        changeStepIndex('next');
        if (stepIndex === 1) {
            changeCollaborate();
        }
    }, [changeStepIndex, changeCollaborate, stepIndex]);
    return (React__default.createElement("div", { className: `${prefixCls}-header` },
        React__default.createElement("div", { className: `${prefixCls}-header-left` },
            React__default.createElement(IconButton, { icon: "close", type: "only-icon", className: `${prefixCls}-close`, onClick: onClose }),
            titleArrs[stepIndex]),
        !isView && (React__default.createElement("div", { className: `${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 };
