import { CustomerListPage } from '@/manager/modules/customer/views/CustomerListPage';
import { ICustomer, ICustomerEditGeneralData } from '@/manager/modules/customer/types/customer-types';
import {
  expectCustomerListPageAndThenDetailsPageToDisplayCorrectData,
  expectCustomerDetailsPageAndThenListPageToDisplayCorrectData,
} from '@/manager/modules/customer/utils/customer-entity-expect-utils';

export async function expectCustomerGeneralInfoToBeEdited(
  startPage: CustomerListPage,
  oldCustomer: ICustomer,
  changes: ICustomerEditGeneralData
): Promise<void> {
  // check old customer data
  const oldDetailsPage = await expectCustomerListPageAndThenDetailsPageToDisplayCorrectData(startPage, oldCustomer);

  // open customer edit general dialog
  const editGeneralDialog = await oldDetailsPage.openCustomerEditGeneralDialog();

  // edit customer
  const newDetailsPage = await editGeneralDialog.edit(changes);

  // construct new customer
  const newCustomer: ICustomer = {
    ...oldCustomer,
    ...changes,
    locations: oldCustomer.locations.concat(changes.locations ?? []),
  };

  // check new customer data
  await expectCustomerDetailsPageAndThenListPageToDisplayCorrectData(newDetailsPage, newCustomer);
}
