import { Injectable } from '@angular/core';
import { assign, cloneDeep } from 'lodash-es';
import { FuseMockApiService, FuseMockApiUtils } from '@fuse/lib/mock-api';
import { branchSetting as branchSettingData } from './data';

@Injectable({
  providedIn: 'root',
})
export class BranchSettingMockApi {
  private _branchSetting: any = branchSettingData;

  /**
   * Constructor
   */
  constructor(private _fuseMockApiService: FuseMockApiService) {
    // Register Mock API handlers
    this.registerHandlers();
  }

  // -----------------------------------------------------------------------------------------------------
  // @ Public methods
  // -----------------------------------------------------------------------------------------------------

  /**
   * Register Mock API handlers
   */
  registerHandlers(): void {
    // -----------------------------------------------------------------------------------------------------
    // @ BasicSetting - GET
    // -----------------------------------------------------------------------------------------------------
    this._fuseMockApiService
      .onGet('api/apps/settings/branch', 300)
      .reply(({ request }) => {
        // Clone the deposits
        let branchSetting: any | null = cloneDeep(this._branchSetting);

        // Return the response
        return [
          200,
          {
            branchSetting,
          },
        ];
      });

    // -----------------------------------------------------------------------------------------------------
    // @ BasicSetting - PATCH
    // -----------------------------------------------------------------------------------------------------
    this._fuseMockApiService
      .onPatch('api/apps/settings/branch')
      .reply(({ request }) => {
        // Get the id and deposit
        const id = request.body.id;
        const branchSetting = cloneDeep(request.body.branchSetting);

        // Prepare the updated basicSetting
        let updatedBasicSetting = null;

        // Find the deposit and update it
        // this._basicSetting.forEach((item, index, bs) => {
        //   if (item.id === id) {
        //     // Update the deposit
        //     basicSetting[index] = assign({}, basicSetting[index], deposit);

        //     // Store the updated deposit
        //     updatedDeposit = deposits[index];
        //   }
        // });

        // Return the response
        return [200, branchSetting];
      });
  }
}
