import {
  DateRangeFilterMode,
  Filter,
  RangeItem,
  Translate,
} from '@wix/bex-core';
import { DateRangePickerState } from './DateRangePickerState';
import { action, makeObservable } from 'mobx';
import { FilterToolbarState } from '../providers';

export interface DateRangeFilterStateParams {
  toolbar: FilterToolbarState;
  translate: Translate;
  filter: Filter<RangeItem<Date>>;
  filterByTime?: boolean;
  mode?: DateRangeFilterMode;
}

export class DateRangeFilterState {
  readonly dateRangePicker;

  constructor(params: DateRangeFilterStateParams) {
    this.dateRangePicker = new DateRangePickerState({
      toolbar: params.toolbar,
      translate: params.translate,
      filter: params.filter,
      filterByTime: params.filterByTime,
      mode: params.mode,
    });

    makeObservable(this, {
      init: action.bound,
    });
  }

  init() {
    const { dateRangePicker } = this;
    const dispose = dateRangePicker.init();

    return () => {
      dispose();
    };
  }
}
