import { Component, inject, Input } from '@angular/core'
import { FormBuilder, ReactiveFormsModule, Validators } from '@angular/forms'
import { DialogRef } from '@ngneat/dialog'
import { TranslocoModule } from '@jsverse/transloco'
import { I18nService } from '../../../core'

@Component({
	selector: 'kadi-current-password-modal',
	templateUrl: './current-password-modal.component.html',
	imports: [ReactiveFormsModule, TranslocoModule],
})
export class CurrentPasswordModalComponent {
	private readonly _formBuilder = inject(FormBuilder)
	private readonly _i18nService = inject(I18nService)

	// TODO: Skipped for migration because:
	//  Your application code writes to the input. This prevents migration.
	@Input() title: string

	private readonly _dialogRef = inject(DialogRef)

	form = this._formBuilder.group({
		password: this._formBuilder.control('', {
			validators: [Validators.required],
			nonNullable: true,
		}),
	})

	constructor() {
		this.title = this._dialogRef.data.title ?? this._i18nService.translate('Enter Current Password')
	}

	submit(): void {
		const password = this.form.value.password
		if (password === undefined) return

		this._dialogRef.close(password)
	}

	close(): void {
		this._dialogRef.close()
	}
}
