import type { ItemRenderParams } from '#/components/mj/mj-table-select'

import {
  parsePrepareParams,
  useItemRender,
} from '#/components/mj/mj-table-select'

import BookMgr from '../book-mgr/home.vue'
import StageMgr from '../stage-mgr/home.vue'
import VersionMgr from '../version-mgr/home.vue'

export function useSearchStage(params: ItemRenderParams) {
  parsePrepareParams(params)
  return useItemRender({
    ...params,
    cmp: StageMgr,
    setData: (item: any, _: any, value: any, label: any) => {
      item.stageId = value
      item.stageName = label
    },
    props: {
      modal: { title: '请选择阶段' },
      cmpProps: {
        selectable: true,
      },
      ...params?.props,
    },
  })
}

export function useSearchVersion(params: ItemRenderParams) {
  parsePrepareParams(params)
  return useItemRender({
    ...params,
    cmp: VersionMgr,
    setData: (item: any, _: any, value: any, label: any) => {
      item.versionId = value
      item.versionName = label
    },
    props: {
      modal: { title: '请选择版本' },
      ...params?.props,
    },
  })
}

export function useSearchStageVersion(params: ItemRenderParams) {
  parsePrepareParams(params)
  const isChangeArray = Array.isArray(params.change)
  const isPropsArray = Array.isArray(params.props)
  const versionRender = useSearchVersion({
    getData: params.getData,
    change: isChangeArray ? (params.change as any)?.[1] : params.change,
    props: {
      disabled: true,
      cmpProps: {
        fixedStage: true,
      },
      ...(isPropsArray ? (params.props as any)?.[1] : params.props),
    },
  })

  const stageRender = useSearchStage({
    getData: params.getData,
    change: isChangeArray ? (params.change as any)?.[0] : params.change,
    nextRender: versionRender,
    props: isPropsArray ? (params.props as any)?.[0] : params.props,
  })
  return {
    stageRender,
    versionRender,
  }
}

export function useSearchBook(params: ItemRenderParams) {
  parsePrepareParams(params)
  return useItemRender({
    ...params,
    cmp: BookMgr,
    setData: (item: any, _: any, value: any, label: any) => {
      item.textbookId = value
      item.textbookName = label
    },
    props: {
      modal: { title: '请选择书本' },
      ...params?.props,
    },
  })
}
