import { inject, ref, type Ref, type Component, type VNode } from 'vue'

export interface FiltrosResumen {
  count: number
  summary: string
  results: number
}

export interface DatasourceCounts {
  [key: string]: number
}

export interface ActiveFilter {
  type: string
  label: string
  value: any
  onRemove: () => void
}

export interface PageSections {
  totalesCafe: boolean
  totalesVerde: boolean
  tablaIngresos: boolean
  top10Clientes: boolean
  graficas: boolean
}

export function useInformeLayout() {
  const setFiltrosResumenFn = inject<(resumen: FiltrosResumen | null) => void>('setFiltrosResumen')
  const setDatasourceCountsFn = inject<(counts: DatasourceCounts) => void>('setDatasourceCounts')
  const setFilteredResultsFn = inject<(results: DatasourceCounts) => void>('setFilteredResults')
  const setActiveFiltersFn = inject<(filters: ActiveFilter[]) => void>('setActiveFilters')
  const setMetadatosNeedUpdateFn = inject<(needsUpdate: boolean) => void>('setMetadatosNeedUpdate')
  const filtrosCollapsedRef = inject<Ref<boolean>>('filtrosCollapsed')
  const metadatosCollapsedRef = inject<Ref<boolean>>('metadatosCollapsed')
  const pageSectionsRef = inject<Ref<PageSections>>('pageSections')

  function setFiltrosResumen(count: number, summary: string, results: number) {
    if (setFiltrosResumenFn) {
      setFiltrosResumenFn({ count, summary, results })
    }
  }

  function clearFiltrosResumen() {
    if (setFiltrosResumenFn) {
      setFiltrosResumenFn(null)
    }
  }

  function setDatasourceCounts(counts: DatasourceCounts) {
    if (setDatasourceCountsFn) {
      setDatasourceCountsFn(counts)
    }
  }

  function setFilteredResults(results: DatasourceCounts) {
    if (setFilteredResultsFn) {
      setFilteredResultsFn(results)
    }
  }

  function setActiveFilters(filters: ActiveFilter[]) {
    if (setActiveFiltersFn) {
      setActiveFiltersFn(filters)
    }
  }

  function setMetadatosNeedUpdate(needsUpdate: boolean) {
    if (setMetadatosNeedUpdateFn) {
      setMetadatosNeedUpdateFn(needsUpdate)
    }
  }

  return {
    setFiltrosResumen,
    clearFiltrosResumen,
    setDatasourceCounts,
    setFilteredResults,
    setActiveFilters,
    setMetadatosNeedUpdate,
    filtrosCollapsed: filtrosCollapsedRef || ref(false),
    metadatosCollapsed: metadatosCollapsedRef || ref(false),
    pageSections: pageSectionsRef || ref({
      totalesCafe: true,
      totalesVerde: true,
      tablaIngresos: true,
      top10Clientes: true,
      graficas: true
    })
  }
}
