import { inject } from '@angular/core';
import { CanDeactivateFn } from '@angular/router';
import { ProjectEditorFacade } from '../facade/project-editor.facade';
import { ProjectEditorPageComponent } from '../pages/project-editor-page.component';
import { TranslateService } from '../../../i18n/translate.service';

export const projectEditorDirtyGuard: CanDeactivateFn<ProjectEditorPageComponent> = () => {
  const facade = inject(ProjectEditorFacade);
  if (!facade.dirty()) {
    return true;
  }
  const translate = inject(TranslateService);
  return window.confirm(translate.t('builder.confirmLeaveUnsaved'));
};
