import { Granularity, QuickFilters } from '../_inputs/granular-date-range/types.js';
import { FilterType } from './filter-input/enum.js';
import { RelativeDateRange } from '../_inputs/date-range/types.js';

interface FilterOption {
  value: string
  text: string
  disabled?: boolean
  selected?: boolean
  sortedAsc?: boolean
}

type FilterValue =
  | TextFilterValue
  | FilterValueWithOptions
  | MultiselectFilterValue
  | DateFilterValue
  | DateRangeFilterValue
  | GranularDateRangeFilterValue

type FilterValueType = string | DateRange | GranularDateRange | null
interface BaseFilterValue {
  text: string
  name: string
  type: FilterType
  value: FilterValueType
  mandatory?: boolean | undefined
  index?: number | undefined
}

interface TextFilterValue extends BaseFilterValue {
  minimumLength?: number | undefined
  pattern?: string | undefined
}

interface FilterValueWithOptions extends BaseFilterValue {
  options: Array<FilterOption>
  staticOptionNameValue?: string
  dynamicResourceEndpoint?: string
}

interface MultiselectFilterValue extends Omit<BaseFilterValue, 'value'>, FilterValueWithOptions {
  value: string | null
  values: string[]
}

interface DateFilterValue extends Omit<BaseFilterValue, 'value'> {
  value: string | null
  min?: string
  max?: string
}

interface DateRangeFilterValue extends Omit<BaseFilterValue, 'value'> {
  value: DateRange
  min?: string
  max?: string
  relativeOptions?: { value: string; text: string; disabled?: boolean }[]
}

interface GranularDateRangeFilterValue extends Omit<BaseFilterValue, 'value'> {
  value: GranularDateRange
  min?: string
  max?: string
  relativeOptions?: { value: string; text: string; disabled?: boolean }[]
  quickFilterOptions?: { value: string; text: string; disabled?: boolean }[]
  granularityOptions?: { value: string; text: string }[]
}

interface DateRange {
  start: string
  end: string
  relative?: RelativeDateRange | undefined
}

interface GranularDateRange extends DateRange {
  granularity: GranularDateRangeGranularityValue
  quickFilter?: GranularDateRangeQuickFilterValue
  partialDate?: PartialDate
}

interface PartialDate {
  start: boolean
  end: boolean
}

interface GranularDateRangeGranularityValue {
  value: Granularity
  display: string
}

interface GranularDateRangeQuickFilterValue {
  value: QuickFilters
  display: string
}

export type { BaseFilterValue, DateFilterValue, DateRange, DateRangeFilterValue, FilterOption, FilterValue, FilterValueType, FilterValueWithOptions, GranularDateRange, GranularDateRangeFilterValue, GranularDateRangeGranularityValue, GranularDateRangeQuickFilterValue, MultiselectFilterValue, PartialDate, TextFilterValue };
