import { Component, Input, Injectable, Directive } from '@angular/core';
import { Location } from '@angular/common';
import { Store } from '@ngrx/store';
import * as mapReducers from '../../reducers/map.reducer';
import { commonReducers,ItemTypeService,IListItem } from '@farmmaps/common';
import * as mapActions from '../../actions/map.actions';
import { tassign } from 'tassign';


@Injectable()
@Directive()
export abstract class AbstractItemListComponent {
  @Input() items: Array<IListItem>
  constructor(public store: Store<mapReducers.State | commonReducers.State>, public itemTypeService: ItemTypeService, private location: Location) {
  }

  handleItemClick(item:IListItem) {
    const newQuery: any = tassign(mapReducers.initialState.query);
    newQuery.itemCode = item.code;
    this.store.dispatch(new mapActions.DoQuery(newQuery));
  }

  handleBackClick(event: MouseEvent) {
    event.preventDefault();
    this.location.back();
  }
}

@Injectable()
@Component({
    selector: 'fm-map-item-list',
    templateUrl: './item-list.component.html',
    styleUrls: ['./item-list.component.scss'],
    standalone: false
})
export class ItemListComponent extends AbstractItemListComponent {

  constructor(store: Store<mapReducers.State | commonReducers.State>, itemTypeService: ItemTypeService,location:Location) {
    super(store, itemTypeService,location);
  }
}
