import { Injectable } from '@angular/core';
import { VATDTO } from '@swagger/oms';
import { State, Action, StateContext, Selector } from '@ngxs/store';
import { CollectingShelfService } from '../../services/collecting-shelf.service';
import { tap } from 'rxjs/operators';
import * as actions from '../actions/vat.actions';

export class VatStateModel {
  vats: VATDTO[];
}

@State<VatStateModel>({
  name: 'vat',
  defaults: {
    vats: [],
  },
})
@Injectable()
export class VatState {
  constructor(private shelfService: CollectingShelfService) {}

  @Selector()
  static getVats(state: VatStateModel) {
    return state.vats;
  }

  @Action(actions.LoadVats)
  loadVats(ctx: StateContext<VatStateModel>, {}: actions.LoadVats) {
    return this.shelfService.getGrossPriceVats().pipe(
      tap((vats: VATDTO[]) => {
        ctx.patchState({ vats });
      })
    );
  }
}
