import { getUrlParam, request } from '@vtx/utils';
import { useEffect } from 'react';
import { useState } from 'react';

const getTenantById = async () => {
    const { tenantId } = getUrlParam();
    let param = {
        parameters: JSON.stringify({ id: tenantId }),
    };
    const data = await request.get('/cloud/management/rest/np/tenant/getTenantById', {
        body: param,
    });
    let mapType,
        mapName,
        coordinate,
        minZoom = 3,
        maxZoom = 19,
        basicData = {},
        wkid = '',
        mapCenter = [];
    if (!!data && !data.result) {
        if ('data' in data) {
            let { longitudeDone, latitudeDone, mapDefJson } = data.data || {};
            if (longitudeDone && latitudeDone) {
                mapCenter = [longitudeDone, latitudeDone];
                if (mapDefJson) {
                    mapDefJson = JSON.parse(mapDefJson);
                    mapDefJson.map(item => {
                        if (item.defaultMap) {
                            if (item.mapType == 'gmap') {
                                basicData = JSON.parse(item.basicData);
                                wkid = item.wkid;
                            }
                            mapType = item.mapType;
                            mapName = item.mapName;
                            coordinate = item.coordinate;
                            minZoom = item.minZoom;
                            maxZoom = item.maxZoom;
                        }
                    });
                }
            }
            let mapObj = {
                basicData,
                wkid,
                mapType,
                mapName,
                coordinate,
                minZoom,
                maxZoom,
                mapCenter,
            };
            return mapObj;
        }
        return {};
    }
};

export default () => {
    const [mapInfo, setMapInfo] = useState({});
    useEffect(() => {
        getTenantById().then(data => {
            setMapInfo(data);
        });
    }, []);

    return { mapInfo };
};
