import { get } from "lodash-es"
import { FC, useCallback, useMemo } from "react"
import { useSelector } from "react-redux"
 import { ViewItemShape } from "@/page/app/components/InspectPanel/PanelSetters/ContainerSetter/ViewsSetter/interface"
import BaseInput from "@/page/app/components/InspectPanel/PanelSetters/InputSetter/BaseInput"
import { getExecutionResult } from "@/redux/currentApp/executionTree/executionSelector"
import { RootState } from "@/store"
import { ContainerDefaultViewKeySetterProps } from "./interface"


const ContainerDefaultViewKeySetter: FC<ContainerDefaultViewKeySetterProps> = (
  props,
) => {
  const {
    handleUpdateMultiAttrDSL,
    handleUpdateOtherMultiAttrDSL,
    widgetDisplayName,
  } = props

  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  const targetComponentProps = useSelector<RootState, Record<string, any>>(
    (rootState) => {
      const executionTree = getExecutionResult(rootState)
      return get(executionTree, widgetDisplayName, {})
    },
  )

  const realViews = useMemo(() => {
    return get(targetComponentProps, "viewList", []) as ViewItemShape[]
  }, [targetComponentProps])

  const linkWidgetDisplayName = useMemo(() => {
    return get(targetComponentProps, "linkWidgetDisplayName")
  }, [targetComponentProps])

  const handleUpdateDefaultView = useCallback(
    (attrPath: string, value: string) => {
      const defaultViewIndex = realViews.findIndex((view) => view.key === value)
      let currentIndex = 0
      let currentKey = realViews[currentIndex].key
      if (defaultViewIndex > -1) {
        currentIndex = defaultViewIndex
        currentKey = realViews[currentIndex].key
      }
      handleUpdateMultiAttrDSL?.({
        [attrPath]: value,
        currentIndex,
        currentKey,
      })
      if (linkWidgetDisplayName && Array.isArray(linkWidgetDisplayName)) {
        linkWidgetDisplayName.forEach((name) => {
          handleUpdateOtherMultiAttrDSL?.(name, {
            currentIndex,
            currentKey,
          })
        })
      }
    },
    [
      realViews,
      linkWidgetDisplayName,
      handleUpdateMultiAttrDSL,
      handleUpdateOtherMultiAttrDSL,
    ],
  )

  return <BaseInput {...props} handleUpdateDsl={handleUpdateDefaultView} />
}

ContainerDefaultViewKeySetter.displayName = "ContainerDefaultViewKeySetter"
export default ContainerDefaultViewKeySetter
