import { inject } from '@angular/core';
import { CanDeactivateFn } from '@angular/router';
import { AdminProductsFacade } from '../facade/admin-products.facade';
import { AdminProductEditorPageComponent } from '../pages/admin-product-editor-page.component';
import { TranslateService } from '../../../../i18n/translate.service';

export const adminProductDirtyGuard: CanDeactivateFn<AdminProductEditorPageComponent> = () => {
  const facade = inject(AdminProductsFacade);
  if (!facade.dirty()) {
    return true;
  }
  const translate = inject(TranslateService);
  return window.confirm(translate.t('adminProducts.confirmLeaveUnsaved'));
};
