import { Component, ChangeDetectionStrategy, inject } from '@angular/core';
import { PickupShelfDetailsBaseComponent } from '../../pickup-shelf-details-base.component';
import { SharedGoodsInOutOrderEditModule } from '@shared/components/goods-in-out';
import { first } from 'rxjs/operators';
import { PickUpShelfOutNavigationService } from '@shared/services/navigation';
import { AsyncPipe } from '@angular/common';
import { DBHOrderItemListItemDTO } from '@generated/swagger/oms-api';

@Component({
  selector: 'page-pickup-shelf-out-edit',
  templateUrl: 'pickup-shelf-out-edit.component.html',
  styleUrls: ['pickup-shelf-out-edit.component.css'],
  changeDetection: ChangeDetectionStrategy.OnPush,
  host: { class: 'page-pickup-shelf-out-edit' },
  imports: [SharedGoodsInOutOrderEditModule, AsyncPipe],
})
export class PickupShelfOutEditComponent extends PickupShelfDetailsBaseComponent {
  shelfOutNavigation = inject(PickUpShelfOutNavigationService);

  constructor() {
    super();

    this.listStore;
  }

  async navigateToShelfOutDetailsPage(changes: Partial<DBHOrderItemListItemDTO>) {
    const orderId = (await this.store.orderItems$.pipe(first()).toPromise())?.find((_) => true)?.orderId;
    const orderNumber = decodeURIComponent(this.activatedRoute?.snapshot?.params?.orderNumber);
    let compartmentCode = changes?.compartmentCode ?? this.activatedRoute?.snapshot?.params?.compartmentCode;
    const processingStatus = changes?.processingStatus ?? this.activatedRoute.snapshot.params.orderItemProcessingStatus;
    let compartmentInfo = changes?.compartmentInfo ?? this.activatedRoute.snapshot.params.compartmentInfo;

    if (compartmentCode) {
      compartmentCode = decodeURIComponent(compartmentCode);
    }

    if (compartmentInfo) {
      compartmentInfo = decodeURIComponent(compartmentInfo);
    }

    await this.router.navigate(
      this.shelfOutNavigation.detailRoute({
        processId: this.listStore.processId,
        item: {
          orderId: orderId ? Number(orderId) : undefined,
          orderNumber,
          compartmentCode,
          processingStatus,
          compartmentInfo,
        },
      }).path,
      { queryParamsHandling: 'preserve' },
    );
  }
}
