import * as Types from "../../../../types/world/worldTypes";
// import { deleteWallItem } from '../../../../services/factoryBuilder/assest/wallAsset/deleteWallItemApi';
import { Socket } from 'socket.io-client';

function DeleteWallItems(
    hoveredDeletableWallItem: Types.RefMesh,
    setWallItems: Types.setWallItemSetState,
    wallItems: Types.wallItems,
    socket: Socket<any>,
    projectId?: string
): void {

    ////////// Deleting the hovered Wall GLTF from thewallItems and also update it in the localstorage //////////

    if (hoveredDeletableWallItem.current && hoveredDeletableWallItem.current) {
        setWallItems([]);
        let WallItemsRef = wallItems;
        const removedItem = WallItemsRef.find((item) => item.model?.uuid === hoveredDeletableWallItem.current?.uuid);
        const Items = WallItemsRef.filter((item) => item.model?.uuid !== hoveredDeletableWallItem.current?.uuid);

        setTimeout(async () => {
            WallItemsRef = Items;
            setWallItems(WallItemsRef);

            const email = localStorage.getItem('email')
            const organization = (email!.split("@")[1]).split(".")[0];
            const userId = localStorage.getItem("userId");

            //REST

            // await deleteWallItem(organization, removedItem?.model?.uuid!, removedItem?.modelName!)

            //SOCKET

            const data = {
                organization: organization,
                modelUuid: removedItem?.model?.uuid!,
                modelName: removedItem?.modelName!,
                socketId: socket.id,projectId,userId
            }

            socket.emit('v1:wallItems:delete', data);

            const WallItemsForStorage = WallItemsRef.map(item => {
                const { model, ...rest } = item;
                return {
                    ...rest,
                    modelUuid: model?.uuid,
                };
            });

            localStorage.setItem("WallItems", JSON.stringify(WallItemsForStorage));
            hoveredDeletableWallItem.current = null;
        }, 50);
    }
}

export default DeleteWallItems;
