import { useState, useEffect, useContext } from 'react'
import { MapContext } from '../MapContext';
import GlobalContext from '../../../../utils/GlobalContext';
import { addGeoserverLayer } from '../utils/helper';
import _ from 'lodash';
const cacheLayers = [];
export default function BusinessLayer(props) {

    const { layerKey, viewer, filterStr } = useContext(MapContext);
    const { layers, initExtent } = useContext(GlobalContext);

    // useEffect(() => {
    //     if (viewer) {
    //         cacheLayers.forEach(layer => {
    //             layer.layerInstance.show = false;
    //         })
    //         if (layerKey) {
    //             const layerOpt = layers[layerKey];
    //             const cacheLayer = cacheLayers.find(l => l.key === layerKey);
    //             if (cacheLayer) {
    //                 cacheLayer.layerInstance.show = true
    //             } else if (layerOpt) {
    //                 cacheLayers.push({
    //                     key: layerKey,
    //                     layerInstance: addGeoserverLayer({
    //                         layerName: layerOpt.layerName,
    //                         extent: initExtent,
    //                         viewer,
    //                         isWms: true
    //                     })
    //                 })
    //             }
    //         }
    //     }

    // }, [layerKey, layers, viewer, initExtent])

    useEffect(() => {
        if (viewer && layerKey) {
            const layerOpt = layers[layerKey];
            const cacheLayer = cacheLayers.find(l => l.key === layerKey);
            if (cacheLayer) {
                viewer.imageryLayers.remove(cacheLayer.layerInstance);
                _.remove(cacheLayers, item => item.key === layerKey);
            }
            cacheLayers.push({
                key: layerKey,
                layerInstance: addGeoserverLayer({
                    layerName: layerOpt.layerName,
                    extent: initExtent,
                    viewer,
                    isWms: true,
                    cql_filter: filterStr || '1=1'
                })
            })
        }
    }, [layerKey, filterStr, viewer, layers, initExtent])

    return <>

    </>;
}