"use client"
import { usePathname } from "next/navigation"
import { useEffect } from "react"
import { getProjectAdminList } from "./api/project"
import { getUserAll, getUserGroupTree } from "./api/user"
import {
  useAllTaskStore,
  useAllUserStore,
  usePermissionStore,
} from "./store/auth"

export function OptStore() {
  //   const detectEnvironment = async () => {
  //     try {
  //       const result = await getName()
  //       console.log("Running in Tauri environment", result)
  //       return true
  //     } catch {
  //       console.log("Not running in Tauri environment")
  //       return false
  //     }
  //   }
  const user_id = usePermissionStore.getState().user_id
  const { needUpdate, setUserOpts, setTreeData, setAdminOpts, setFlag } =
    useAllUserStore()
  const pathname = usePathname()

  useEffect(() => {
    if (user_id) {
      const getData = async () => {
        const res = await getUserAll()
        setUserOpts(res)
        const gRes = await getUserGroupTree()
        const getTreeData = (data: any, parentHasDisabledChild = false) => {
          let hasDisabledChild = parentHasDisabledChild

          const newData: any = {
            title: data.name,
            value: data.id,
            key: data.id,
            children: [],
            disabled: false,
          }

          if (data.members && data.members.length) {
            data.members.forEach((member: any) => {
              newData.children.push({
                title: member.name,
                value: member.id,
                key: member.id,
              })
            })
          }

          if (data.children && data.children.length) {
            data.children.forEach((child: any) => {
              const childData = getTreeData(child, hasDisabledChild)
              newData.children.push(childData)
              if (childData.disabled) {
                hasDisabledChild = true
              }
            })
          }
          if (
            data.id.includes("-") &&
            (!data.members || !data.members.length) &&
            (!data.children || !data.children.length || hasDisabledChild)
          ) {
            newData.disabled = true
          }

          return newData
        }
        let arr: any[] = []
        gRes.forEach((g: any) => {
          arr.push(getTreeData(g))
        })
        const aRes = await getProjectAdminList()
        setAdminOpts(aRes)
        setTreeData(arr)
        setFlag(false)
      }
      if (needUpdate) getData()
    }
  }, [user_id, setTreeData, setUserOpts, needUpdate, setFlag, setAdminOpts])

  // 全局监听 重置项目详情中任务列表搜索缓存
  useEffect(() => {
    if (pathname !== "/project/info/detail" && pathname !== "/label") {
      useAllTaskStore.getState().resetParams()
    }
  }, [pathname])

  //   // tauri://close-requested
  //   useEffect(() => {
  //     ;(async () => {
  //       if (await detectEnvironment()) {
  //         const window = getCurrentWindow()
  //         const unlisten = await window.onCloseRequested(async (_event) => {
  //           usePermissionStore.getState().logout()
  //           // event.preventDefault();
  //         })

  //         return () => {
  //           unlisten()
  //         }
  //       }
  //     })()
  //   }, [])

  return <></>
}
