import { Injectable } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms';

import { ISecurityGroupPermission, NewSecurityGroupPermission } from '../security-group-permission.model';

/**
 * A partial Type with required key is used as form input.
 */
type PartialWithRequiredKeyOf<T extends { id: unknown }> = Partial<Omit<T, 'id'>> & { id: T['id'] };

/**
 * Type for createFormGroup and resetForm argument.
 * It accepts ISecurityGroupPermission for edit and NewSecurityGroupPermissionFormGroupInput for create.
 */
type SecurityGroupPermissionFormGroupInput = ISecurityGroupPermission | PartialWithRequiredKeyOf<NewSecurityGroupPermission>;

type SecurityGroupPermissionFormDefaults = Pick<NewSecurityGroupPermission, 'id'>;

type SecurityGroupPermissionFormGroupContent = {
  id: FormControl<ISecurityGroupPermission['id'] | NewSecurityGroupPermission['id']>;
  version: FormControl<ISecurityGroupPermission['version']>;
  securityGroup: FormControl<ISecurityGroupPermission['securityGroup']>;
  securityResource: FormControl<ISecurityGroupPermission['securityResource']>;
  createPermission: FormControl<ISecurityGroupPermission['createPermission']>;
  readPermission: FormControl<ISecurityGroupPermission['readPermission']>;
  updatePermission: FormControl<ISecurityGroupPermission['updatePermission']>;
  deletePermission: FormControl<ISecurityGroupPermission['deletePermission']>;
};

export type SecurityGroupPermissionFormGroup = FormGroup<SecurityGroupPermissionFormGroupContent>;

@Injectable({ providedIn: 'root' })
export class SecurityGroupPermissionFormService {
  createSecurityGroupPermissionFormGroup(securityGroupPermission: SecurityGroupPermissionFormGroupInput = { id: null }): SecurityGroupPermissionFormGroup {
    const securityGroupPermissionRawValue = {
      ...this.getFormDefaults(),
      ...securityGroupPermission,
    };
	
    return new FormGroup<SecurityGroupPermissionFormGroupContent>({
      id: new FormControl(
        { value: securityGroupPermissionRawValue.id, disabled: true },
        {
          nonNullable: true,
          validators: [Validators.required],
        },
      ),
      version: new FormControl(securityGroupPermissionRawValue.version),
      
      securityGroup: new FormControl(securityGroupPermissionRawValue.securityGroup),
      securityResource: new FormControl(securityGroupPermissionRawValue.securityResource),
      createPermission: new FormControl(securityGroupPermissionRawValue.createPermission),
      readPermission: new FormControl(securityGroupPermissionRawValue.readPermission),
      updatePermission: new FormControl(securityGroupPermissionRawValue.updatePermission),
      deletePermission: new FormControl(securityGroupPermissionRawValue.deletePermission),
      
    });
  }

  private getFormDefaults(): SecurityGroupPermissionFormDefaults {
    return {
      id: null,
    };
  }
}
