import "@babel/polyfill";
import md5 from 'md5';
import { assign, getSet, setData, depthCopy } from './tool';
/**
 * 数据结构配置
 */
import Layout from './data/Layout';
import FirmExplanation from './data/FirmExplanation';
import MapDataNav from "./data/MapDataNav";
import MapDataNavEcharts from "./data/MapDataNavEcharts";
import YieldScale from "./data/YieldScale";
import SalesRatio from "./data/SalesRatio";
import YearOutputSales from './data/YearOutputSales';
import InfoCount from './data/InfoCount';
import CropCount from './data/CropCount';
import ParkList from './data/ParkList';
import PovertyAlleviation from './data/PovertyAlleviation';
import PovertyAlleviationResult from './data/PovertyAlleviationResult';
import PovertyAlleviationArticle from './data/PovertyAlleviationArticle';
import PovertyAlleviationArticleItem from './data/PovertyAlleviationArticleItem';
import Weather from './data/Weather';
import ParkDetailMenu from './data/ParkDetailMenu';
import ParkDetailsInfo from './data/ParkDetailsInfo';
import ParkDetailsInfo2 from './data/ParkDetailsInfo2';
import MapTooLNav from "./data/MapTooLNav";
import MonitorData from './data/MonitorData';
import ParkDetailAMap from './data/ParkDetailAMap';
import ThingsPiontData from './data/ThingsPiontData';
import ThingsTypeData from './data/ThingsTypeData';
import ThingsEchartsData from './data/ThingsEchartsData';
import MapPointData from './data/MapPointData/index';

import MapDataNavTable from "./data/MapDataNavTable";
import MapDataNavTable1 from "./data/MapDataNavTable1";
import merchantsData from "./data/merchantsData";
import HorizontalListTitleData from "./data/HorizontalListTitleData";
import DropDownListData from "./data/DropDownListData";
import productListData from "./data/productListData";
import classificationListData from "./data/classificationListData";
import ProductDetailsData from "./data/ProductDetailsData";
import leftMenuData from "./data/leftMenuData";
import childMenuData from "./data/childMenuData";
import RightAutoListData from "./data/RightAutoListData";
import LeftRoundChart from "./data/LeftRoundChart";
import RightRoundChart from "./data/RightRoundChart";
import WarningData from "./data/WarningData";
import FloatWindowData from "./data/FloatWindowData";
import MapPointData1 from "./data/MapPointData1";
//数据接口
const Initdata = {
    //天气
    Weather,
    //整个框架数据开始
    Layout,
    //公司介绍
    FirmExplanation,
    //地图与中图表数据导航
    MapDataNav,
    //中图表
    MapDataNavEcharts,
    //产品产量比例
    YieldScale,
    //产品销售比率
    SalesRatio,
    //年生产销售数据
    YearOutputSales,
    //坝区统计
    InfoCount,
    //公司种植统计
    CropCount,
    //园区分布列表
    ParkList,
    //扶贫数据
    PovertyAlleviation,
    //扶贫成就
    PovertyAlleviationResult,
    //扶贫文章
    PovertyAlleviationArticle,
    //文章详情
    PovertyAlleviationArticleItem,
    //园区详情左菜单
    ParkDetailMenu,
    //园区情况 信息
    ParkDetailsInfo,
    ParkDetailsInfo2,
    //地图工具导航
    MapTooLNav,
    //监控列表
    MonitorData,
    //园区详情地图
    ParkDetailAMap,
    //物联监测点
    ThingsPiontData,
    //监测类型项数据
    ThingsTypeData,
    //监测类型单项图表数据
    ThingsEchartsData,
    //态势 湄潭海量点 地图 
    MapPointData,
    //态势 下部 表格
    MapDataNavTable,
    MapDataNavTable1,
    merchantsData,
    HorizontalListTitleData,
    DropDownListData,
    productListData,
    classificationListData,
    ProductDetailsData,
    leftMenuData,
    childMenuData,
    RightAutoListData,
    LeftRoundChart,
    RightRoundChart,
    WarningData,
    FloatWindowData,
    MapPointData1
}
//参数转换
const par = e => {
    const arr = Object
        .keys(e)
        .map(x => {
            return `${x}=${e[x]}`;
        });
    return !!arr.length
        ? `?${arr.join("&")}`
        : '';
}
//获取模板结构与默认数据
export const getData = async data => {
    // port 模板名称 - 作为参数 传到后台 获取对应组件下数据
    const {
        id = '', //数据id
        pId = '', //结构id
        port = '', //模板id
        mapId = '', //大屏id
        mId = ''
    } = data;
    //取得结构默认数据
    let defdata = {
        ...depthCopy(Initdata[port]),
        ...data
    };

    (defdata.data || {}).id = id;
    /**
	 * pId 框架+父页 唯一ID
	 * mapId 当前大屏ID
	 * mId 同页多组件 用这个ID来区分
	 * id 数据ID
	 */
    // 读取修改数据 mId 同页多组件 用这个ID来区分
    // console.log("读", port,id, md5(pId + port + mapId + mId + id));
    const res = await fetch(`/htdata/map/findModuleData${par({
        moduleCode: md5(pId + port + mapId + mId + id),
        mapId
    })}`, {
        method: 'GET',
        headers: {
            'Content-Type': 'application/json'
        }
    });
    const res_1 = await res.json();
    const body = res_1.retBody || {};
    let returnData = {};
    if ((body.content + "") == "null") {
        returnData = {
            config: {
                ...defdata
            }
        };
    } else {
        try {
            returnData = {
                config: {
                    ...defdata,
                    data: JSON.parse(body.content)
                }
            };
        } catch (e_1) {
            returnData = {
                config: {
                    ...defdata
                }
            };
        }
    }
    const {
        config: {
            data: __data = {}
        }
    } = returnData, url = (__data.dataInterface || {}).url;
    //如果存在接口数据 - 则读取接口数据
    if (url) {
        //恢复配置参数对象
        let params = {};
        (((__data || {}).dataInterface || {}).params || []).map((x, i) => {
            x.value && (params[x.value] = (data.params || {})[x.value] || '');
        });
        try {
            const res = await fetch(`${url}${par(params)}`).then(res => res.json()).catch(error => {
                return {

                }
            });
            //在返回数据中查找数据关联集合
            const AimsData = {};
            (((__data || {}).dataInterface || {}).dataSource || []).map(x => {
                const fun = (m, n) => {
                    //key关联
                    const type = Object.prototype.toString.call(n);
                    const typem = Object.prototype.toString.call(m);
                    let data = null;
                    if (type === "[object Object]") {
                        data = {};
                        m = typem == "[object Object]" ? m : {};
                    } else if (type === "[object Array]") {
                        data = [];
                        m = typem == "[object Array]" ? m : [];
                    }
                    data && (x.keyRelated + "").split("|").map(x => {
                        const keys = (x + "").split(".");
                        if (keys.length == 2) {
                            data = assign(data, n.map(c => {
                                const _c = {};
                                _c[keys[1]] = c[keys[0]];
                                return {
                                    ..._c
                                };
                            }));
                        }
                    });
                    return assign(depthCopy(n), assign(depthCopy(m), data));
                };
                //取得数据源
                setData(AimsData, x.AimsKey, getSet(res, x.SourceKey), fun);
            });
            returnData = assign(returnData, {
                config: {
                    ...AimsData
                }
            });
        } catch (error) {

        }
    }
    return returnData;
}