import {
  ChangeDetectionStrategy,
  Component,
  computed,
  inject,
  input,
  viewChild,
} from '@angular/core';
import { ReturnDetailsOrderGroupComponent } from '../return-details-order-group/return-details-order-group.component';
import { ReturnDetailsOrderGroupDataComponent } from '../return-details-order-group-data/return-details-order-group-data.component';
import { NgIcon, provideIcons } from '@ng-icons/core';
import { isaActionPlus, isaActionMinus } from '@isa/icons';
import { ReturnDetailsOrderGroupItemComponent } from '../return-details-order-group-item/return-details-order-group-item.component';
import { ReceiptListItem, ReturnDetailsStore } from '@isa/oms/data-access';
import { ExpandableDirective, ExpandableDirectives } from '@isa/ui/expandable';
import { TextButtonComponent } from '@isa/ui/buttons';
import { ProgressBarComponent } from '@isa/ui/progress-bar';

@Component({
  selector: 'oms-feature-return-details-lazy',
  templateUrl: './return-details-lazy.component.html',
  styleUrls: ['./return-details-lazy.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
  standalone: true,
  imports: [
    ReturnDetailsOrderGroupComponent,
    ReturnDetailsOrderGroupDataComponent,
    ReturnDetailsOrderGroupItemComponent,
    NgIcon,
    ExpandableDirectives,
    TextButtonComponent,
    ProgressBarComponent,
  ],
  providers: [provideIcons({ isaActionPlus, isaActionMinus })],
})
export class ReturnDetailsLazyComponent {
  #store = inject(ReturnDetailsStore);
  receipt = input.required<ReceiptListItem>();

  receiptMap = this.#store.receiptsEntityMap;

  exbandable = viewChild(ExpandableDirective);

  receiptId = computed(() => {
    const ex = this.exbandable();
    if (!ex) {
      return;
    }

    if (!ex.expanded()) {
      return;
    }
    return this.receipt().id;
  });

  receiptResource = this.#store.receiptResource(this.receiptId);
}
