import {AnalyticalScope, Behavior, Control, ControlWithValue, Visible,} from "@webbpm/base-package";
import {HttpClient} from "@angular/common/http";


@AnalyticalScope(Control)
export class FieldChecker extends Behavior {
  private client: HttpClient;
  private control: ControlWithValue;

  initialize() {
    this.client = this.injector.get(HttpClient);
    this.control = this.getScript('component.ControlWithValue');
  }

  @Visible()
  userExists(login: string) {
    return this.client.get<boolean>(`user/exists?login=${encodeURIComponent(login)}`)
        .toPromise()
        .then((exists: boolean) => {
          this.control.setValue(exists);
        })
        .catch((error) => {
          console.error("Ошибка при проверке пользователя:", error);
          this.control.setValue(null);
        });
  }
}