// Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

import { provideLocal } from '@vueuse/shared'
import { isEqual } from 'lodash-es'
import { computed, ref, watch, type Ref } from 'vue'

import emitter from '#shared/utils/emitter.ts'

import { SidebarName } from '#desktop/components/layout/types.ts'
import { useSidebarDisplay } from '#desktop/components/layout/useSidebarDisplay.ts'

import { useTicketSidebarPlugins } from '../components/TicketSidebar/plugins/index.ts'

import { TICKET_SIDEBAR_SYMBOL } from './useTicketSidebar.ts'

import type { TicketSidebarPlugin } from '../components/TicketSidebar/plugins/types.ts'
import type { TicketSidebarContext } from '../types/sidebar.ts'

export const useProvideTicketSidebar = (context: Ref<TicketSidebarContext>) => {
  const shownSidebars = ref<Record<string, boolean>>({})
  const switchedSidebar = ref<string>()

  const showSidebar = (sidebar: string) => {
    shownSidebars.value[sidebar] = true
  }

  const hideSidebar = (sidebar: string) => {
    shownSidebars.value[sidebar] = false
  }

  const { toggleSidebar } = useSidebarDisplay(SidebarName.TicketContent)

  const switchSidebar = (newSidebar: string) => {
    switchedSidebar.value = newSidebar

    toggleSidebar(false)
  }

  const sidebarPlugins = useTicketSidebarPlugins(context.value.screenType)

  const availableSidebarPlugins = computed<Record<string, TicketSidebarPlugin>>(
    (currentAvailableSidebarPlugins) => {
      const newCurrentSidebarPlugins = Object.fromEntries(
        Object.entries(sidebarPlugins)
          .filter(([, sidebarPlugin]) => sidebarPlugin.views.includes(context.value.view))
          .filter(([, sidebarPlugin]) =>
            typeof sidebarPlugin.available === 'function'
              ? sidebarPlugin.available(context.value)
              : true,
          ),
      )

      if (
        currentAvailableSidebarPlugins &&
        isEqual(currentAvailableSidebarPlugins, newCurrentSidebarPlugins)
      )
        return currentAvailableSidebarPlugins

      return newCurrentSidebarPlugins
    },
  )

  const activeSidebar = computed<string | null>(() => {
    if (!Object.keys(availableSidebarPlugins.value)?.length) return null
    if (
      switchedSidebar.value &&
      availableSidebarPlugins.value[switchedSidebar.value] &&
      shownSidebars.value[switchedSidebar.value]
    )
      return switchedSidebar.value

    const sidebar = Object.entries(availableSidebarPlugins.value).find(
      ([sidebar]) => shownSidebars.value[sidebar],
    )?.[0]

    return sidebar === undefined ? ' ' : sidebar // ' ' is a fallback value for a non-selectable sidebar to prevent flickering if sidebar is loading
  })

  const hasSidebar = computed(() => Boolean(activeSidebar.value))

  watch(hasSidebar, () => {
    emitter.emit('resize-layout')
  })

  provideLocal(TICKET_SIDEBAR_SYMBOL, {
    shownSidebars,
    activeSidebar,
    availableSidebarPlugins,
    sidebarPlugins,
    hasSidebar,
    showSidebar,
    hideSidebar,
    switchSidebar,
  })
}
