import { defaultBaseStateData, EntityState, EntityStore } from '@/core/state';
import { IPartnerInfoResponse } from '@/domains/partner/models';
import { PartnerService } from '@/domains/partner/services/main.service';
import { inject, Injectable } from '@angular/core';
import { catchError, finalize } from 'rxjs';

interface PartnerInfoState extends EntityState<IPartnerInfoResponse> {}

@Injectable({
  providedIn: 'root',
})
export class PartnerInfoStore extends EntityStore<IPartnerInfoResponse, PartnerInfoState> {
  private readonly service = inject(PartnerService);

  constructor() {
    super(defaultBaseStateData);
  }

  getData() {
    this.patchState({ loading: true });
    this.service
      .getInfo()
      .pipe(
        finalize(() => {
          this.patchState({ loading: false });
        }),
        catchError((error) => {
          this.setError(error);
          throw error;
        }),
      )
      .subscribe((entity) => {
        this.setEntity(entity);
      });
  }

  override reset(): void {
    this.setState({ ...defaultBaseStateData });
  }
}
