<commit_before>import { Component, EventEmitter, Input, OnDestroy, OnInit, Output } from '@angular/core';
import { AppLocalization } from '@kaltura-ng/kaltura-common/localization/app-localization.service';
import { PopupWidgetComponent } from '@kaltura-ng/kaltura-ui/popup-widget/popup-widget.component';
import { AbstractControl, FormBuilder, FormGroup, Validators } from '@angular/forms';
import { KalturaSiteRestrictionType } from 'kaltura-ngx-client/api/types/KalturaSiteRestrictionType';
import { KalturaCountryRestrictionType } from 'kaltura-ngx-client/api/types/KalturaCountryRestrictionType';
import { KalturaIpAddressRestrictionType } from 'kaltura-ngx-client/api/types/KalturaIpAddressRestrictionType';
import { KalturaLimitFlavorsRestrictionType } from 'kaltura-ngx-client/api/types/KalturaLimitFlavorsRestrictionType';
import { AccessControlProfilesStore, ExtendedKalturaAccessControl } from '../profiles-store/profiles-store.service';

import { BrowserService } from 'app-shared/kmc-shell';
import { KalturaAccessControl } from 'kaltura-ngx-client/api/types/KalturaAccessControl';
import { KalturaSiteRestriction } from 'kaltura-ngx-client/api/types/KalturaSiteRestriction';
import { KalturaCountryRestriction } from 'kaltura-ngx-client/api/types/KalturaCountryRestriction';
import { KalturaIpAddressRestriction } from 'kaltura-ngx-client/api/types/KalturaIpAddressRestriction';
import { KalturaLimitFlavorsRestriction } from 'kaltura-ngx-client/api/types/KalturaLimitFlavorsRestriction';
import { KalturaSessionRestriction } from 'kaltura-ngx-client/api/types/KalturaSessionRestriction';
import { KalturaPreviewRestriction } from 'kaltura-ngx-client/api/types/KalturaPreviewRestriction';
import { globalConfig } from 'config/global';
import { KalturaLogger } from '@kaltura-ng/kaltura-logger/kaltura-logger.service';

export interface AccessControlAutocompleteItem {
  value: string;
  __tooltip: any;
  __class?: string;
}

@Component({
  selector: 'kAccessControlProfilesEditProfile',
  templateUrl: './edit-profile.component.html',
  styleUrls: ['./edit-profile.component.scss'],
  providers: [KalturaLogger.createLogger('EditProfileComponent')]
})
export class EditProfileComponent implements OnInit, OnDestroy {
  @Input() parentPopup: PopupWidgetComponent;

  @Input() profile: ExtendedKalturaAccessControl | null;

  @Output() onSave = new EventEmitter<KalturaAccessControl>();

  private _profile: ExtendedKalturaAccessControl = null;
  public _headerTitle: string;

  public _ipsFormatError = false;
  public _domainsFormatError = false;

  public get _saveBtnDisabled(): boolean {
    return this._domainsFormatError || this._ipsFormatError || this._nameField.invalid;
  }

  public _countryCodes: { value: string }[] = globalConfig.client.countriesList.map(code => {
    return {
      value: code, label: this._appLocalization.get(`countries.${code}`)
    }
  });

  public _profileForm: FormGroup;
  public _nameField: AbstractControl;
  public _descriptionField: AbstractControl;
  public _domainsTypeField: AbstractControl;
  public _allowedDomainsField: AbstractControl;
  public _restrictedDomainsField: AbstractControl;
  public _countriesTypeField: AbstractControl;
  public _allowedCountriesField: AbstractControl;
  public _restrictedCountriesField: AbstractControl;
  public _ipsTypeField: AbstractControl;
  public _allowedIpsField: AbstractControl;
  public _restrictedIpsField: AbstractControl;
  public _flavorsTypeField: AbstractControl;
  public _allowedFlavorsField: AbstractControl;
  public _restrictedFlavorsField: AbstractControl;
  public _secureVideoField: AbstractControl;
  public _allowPreviewField: AbstractControl;
  public _previewField: AbstractControl;

  public _siteRestrictionType = KalturaSiteRestrictionType;
  public _countryRestrictionType = KalturaCountryRestrictionType;
  public _ipAddressRestrictionType = KalturaIpAddressRestrictionType;
  public _flavorsRestrictionType = KalturaLimitFlavorsRestrictionType;

  constructor(private _appLocalization: AppLocalization,
              private _browserService: BrowserService,
              private _fb: FormBuilder,
              private _logger: KalturaLogger,
              public _store: AccessControlProfilesStore) {
    this._convertDomainsUserInputToValidValue = this._convertDomainsUserInputToValidValue.bind(this);
    this._convertIpsUserInputToValidValue = this._convertIpsUserInputToValidValue.bind(this);
    this._buildForm();
  }

  ngOnInit() {
    this._prepare();
  }

  ngOnDestroy() {

  }

  private _prepare(): void {
    if (this.profile) {
      this._logger.info(`enter edit profile mode`, { id: this.profile.id, name: this.profile.name });
      this._profile = this.profile;
      this._setInitialValue(this._profile);
      this._headerTitle = this._appLocalization.get('applications.settings.accessControl.editAccessControlProfile');
    } else {
      this._logger.info(`enter new profile mode`);
      this._headerTitle = this._appLocalization.get('applications.settings.accessControl.addAccessControlProfile');
    }
  }

  private _setInitialValue(profile: ExtendedKalturaAccessControl): void {
    let domainsType = null;
    let allowedDomains = [];
    let restrictedDomains = [];
    if (profile.view.domain) {
      const domain = profile.view.domain;
      const isAuthorized = domain.isAuthorized;
      domainsType = isAuthorized ? KalturaSiteRestrictionType.allowSiteList : KalturaSiteRestrictionType.restrictSiteList;
      allowedDomains = isAuthorized ? domain.details.map(value => ({ value, __tooltip: value })) : [];
      restrictedDomains = !isAuthorized ? domain.details.map(value => ({ value, __tooltip: value })) : [];
    }

    let countriesType = null;
    let allowedCountries = [];
    let restrictedCountries = [];
    if (profile.view.countries) {
      const countries = profile.view.countries;
      const isAuthorized = countries.isAuthorized;
      countriesType = isAuthorized ? KalturaCountryRestrictionType.allowCountryList : KalturaCountryRestrictionType.restrictCountryList;
      allowedCountries = isAuthorized ? countries.details : [];
      restrictedCountries = !isAuthorized ? countries.details : [];
    }

    let ipsType = null;
    let allowedIps = [];
    let restrictedIps = [];
    if (profile.view.ips) {
      const ips = profile.view.ips;
      const isAuthorized = ips.isAuthorized;
      ipsType = isAuthorized ? KalturaIpAddressRestrictionType.allowList : KalturaIpAddressRestrictionType.restrictList;
      allowedIps = isAuthorized ? ips.details.map(value => ({ value, __tooltip: value })) : [];
      restrictedIps = !isAuthorized ? ips.details.map(value => ({ value, __tooltip: value })) : [];
    }

    let flavorsType = null;
    let allowedFlavors = [];
    let restrictedFlavors = [];
    if (profile.view.flavors) {
      const flavors = profile.view.flavors;
      const isAuthorized = flavors.isAuthorized;
      flavorsType = isAuthorized ? KalturaLimitFlavorsRestrictionType.allowList : KalturaLimitFlavorsRestrictionType.restrictList;
      allowedFlavors = isAuthorized ? flavors.details.map(item => item.id) : [];
      restrictedFlavors = !isAuthorized ? flavors.details.map(item => item.id) : [];
    }

    let secureVideo = false;
    let allowPreview = false;
    let preview = 0;
    if (profile.view.advancedSecurity) {
      const advancedSecurity = profile.view.advancedSecurity;
      secureVideo = advancedSecurity.details.secureVideo;
      preview = advancedSecurity.details.preview;
      allowPreview = !!preview;
    }

    this._profileForm.setValue({
      name: profile.name,
      description: profile.description,
      domainsType,
      allowedDomains,
      restrictedDomains,
      countriesType,
      allowedCountries,
      restrictedCountries,
      ipsType,
      allowedIps,
      restrictedIps,
      flavorsType,
      allowedFlavors,
      restrictedFlavors,
      secureVideo,
      allowPreview,
      preview
    });
  }<commit_msg>fix(settings/access-control-profiles): disable profile name editing for the default profile<commit_after>import { Component, EventEmitter, Input, OnDestroy, OnInit, Output } from '@angular/core';
import { AppLocalization } from '@kaltura-ng/kaltura-common/localization/app-localization.service';
import { PopupWidgetComponent } from '@kaltura-ng/kaltura-ui/popup-widget/popup-widget.component';
import { AbstractControl, FormBuilder, FormGroup, Validators } from '@angular/forms';
import { KalturaSiteRestrictionType } from 'kaltura-ngx-client/api/types/KalturaSiteRestrictionType';
import { KalturaCountryRestrictionType } from 'kaltura-ngx-client/api/types/KalturaCountryRestrictionType';
import { KalturaIpAddressRestrictionType } from 'kaltura-ngx-client/api/types/KalturaIpAddressRestrictionType';
import { KalturaLimitFlavorsRestrictionType } from 'kaltura-ngx-client/api/types/KalturaLimitFlavorsRestrictionType';
import { AccessControlProfilesStore, ExtendedKalturaAccessControl } from '../profiles-store/profiles-store.service';

import { BrowserService } from 'app-shared/kmc-shell';
import { KalturaAccessControl } from 'kaltura-ngx-client/api/types/KalturaAccessControl';
import { KalturaSiteRestriction } from 'kaltura-ngx-client/api/types/KalturaSiteRestriction';
import { KalturaCountryRestriction } from 'kaltura-ngx-client/api/types/KalturaCountryRestriction';
import { KalturaIpAddressRestriction } from 'kaltura-ngx-client/api/types/KalturaIpAddressRestriction';
import { KalturaLimitFlavorsRestriction } from 'kaltura-ngx-client/api/types/KalturaLimitFlavorsRestriction';
import { KalturaSessionRestriction } from 'kaltura-ngx-client/api/types/KalturaSessionRestriction';
import { KalturaPreviewRestriction } from 'kaltura-ngx-client/api/types/KalturaPreviewRestriction';
import { globalConfig } from 'config/global';
import { KalturaLogger } from '@kaltura-ng/kaltura-logger/kaltura-logger.service';

export interface AccessControlAutocompleteItem {
  value: string;
  __tooltip: any;
  __class?: string;
}

@Component({
  selector: 'kAccessControlProfilesEditProfile',
  templateUrl: './edit-profile.component.html',
  styleUrls: ['./edit-profile.component.scss'],
  providers: [KalturaLogger.createLogger('EditProfileComponent')]
})
export class EditProfileComponent implements OnInit, OnDestroy {
  @Input() parentPopup: PopupWidgetComponent;

  @Input() profile: ExtendedKalturaAccessControl | null;

  @Output() onSave = new EventEmitter<KalturaAccessControl>();

  private _profile: ExtendedKalturaAccessControl = null;
  public _headerTitle: string;

  public _ipsFormatError = false;
  public _domainsFormatError = false;

  public get _saveBtnDisabled(): boolean {
    return this._domainsFormatError || this._ipsFormatError || this._nameField.invalid;
  }

  public _countryCodes: { value: string }[] = globalConfig.client.countriesList.map(code => {
    return {
      value: code, label: this._appLocalization.get(`countries.${code}`)
    }
  });

  public _profileForm: FormGroup;
  public _nameField: AbstractControl;
  public _descriptionField: AbstractControl;
  public _domainsTypeField: AbstractControl;
  public _allowedDomainsField: AbstractControl;
  public _restrictedDomainsField: AbstractControl;
  public _countriesTypeField: AbstractControl;
  public _allowedCountriesField: AbstractControl;
  public _restrictedCountriesField: AbstractControl;
  public _ipsTypeField: AbstractControl;
  public _allowedIpsField: AbstractControl;
  public _restrictedIpsField: AbstractControl;
  public _flavorsTypeField: AbstractControl;
  public _allowedFlavorsField: AbstractControl;
  public _restrictedFlavorsField: AbstractControl;
  public _secureVideoField: AbstractControl;
  public _allowPreviewField: AbstractControl;
  public _previewField: AbstractControl;

  public _siteRestrictionType = KalturaSiteRestrictionType;
  public _countryRestrictionType = KalturaCountryRestrictionType;
  public _ipAddressRestrictionType = KalturaIpAddressRestrictionType;
  public _flavorsRestrictionType = KalturaLimitFlavorsRestrictionType;

  constructor(private _appLocalization: AppLocalization,
              private _browserService: BrowserService,
              private _fb: FormBuilder,
              private _logger: KalturaLogger,
              public _store: AccessControlProfilesStore) {
    this._convertDomainsUserInputToValidValue = this._convertDomainsUserInputToValidValue.bind(this);
    this._convertIpsUserInputToValidValue = this._convertIpsUserInputToValidValue.bind(this);
    this._buildForm();
  }

  ngOnInit() {
    this._prepare();
  }

  ngOnDestroy() {

  }

  private _prepare(): void {
    if (this.profile) {
      this._logger.info(`enter edit profile mode`, { id: this.profile.id, name: this.profile.name });
      this._profile = this.profile;
      this._setInitialValue(this._profile);
      this._headerTitle = this._appLocalization.get('applications.settings.accessControl.editAccessControlProfile');
    } else {
      this._logger.info(`enter new profile mode`);
      this._headerTitle = this._appLocalization.get('applications.settings.accessControl.addAccessControlProfile');
    }
  }

  private _setInitialValue(profile: ExtendedKalturaAccessControl): void {
    let domainsType = null;
    let allowedDomains = [];
    let restrictedDomains = [];
    if (profile.view.domain) {
      const domain = profile.view.domain;
      const isAuthorized = domain.isAuthorized;
      domainsType = isAuthorized ? KalturaSiteRestrictionType.allowSiteList : KalturaSiteRestrictionType.restrictSiteList;
      allowedDomains = isAuthorized ? domain.details.map(value => ({ value, __tooltip: value })) : [];
      restrictedDomains = !isAuthorized ? domain.details.map(value => ({ value, __tooltip: value })) : [];
    }

    let countriesType = null;
    let allowedCountries = [];
    let restrictedCountries = [];
    if (profile.view.countries) {
      const countries = profile.view.countries;
      const isAuthorized = countries.isAuthorized;
      countriesType = isAuthorized ? KalturaCountryRestrictionType.allowCountryList : KalturaCountryRestrictionType.restrictCountryList;
      allowedCountries = isAuthorized ? countries.details : [];
      restrictedCountries = !isAuthorized ? countries.details : [];
    }

    let ipsType = null;
    let allowedIps = [];
    let restrictedIps = [];
    if (profile.view.ips) {
      const ips = profile.view.ips;
      const isAuthorized = ips.isAuthorized;
      ipsType = isAuthorized ? KalturaIpAddressRestrictionType.allowList : KalturaIpAddressRestrictionType.restrictList;
      allowedIps = isAuthorized ? ips.details.map(value => ({ value, __tooltip: value })) : [];
      restrictedIps = !isAuthorized ? ips.details.map(value => ({ value, __tooltip: value })) : [];
    }

    let flavorsType = null;
    let allowedFlavors = [];
    let restrictedFlavors = [];
    if (profile.view.flavors) {
      const flavors = profile.view.flavors;
      const isAuthorized = flavors.isAuthorized;
      flavorsType = isAuthorized ? KalturaLimitFlavorsRestrictionType.allowList : KalturaLimitFlavorsRestrictionType.restrictList;
      allowedFlavors = isAuthorized ? flavors.details.map(item => item.id) : [];
      restrictedFlavors = !isAuthorized ? flavors.details.map(item => item.id) : [];
    }

    let secureVideo = false;
    let allowPreview = false;
    let preview = 0;
    if (profile.view.advancedSecurity) {
      const advancedSecurity = profile.view.advancedSecurity;
      secureVideo = advancedSecurity.details.secureVideo;
      preview = advancedSecurity.details.preview;
      allowPreview = !!preview;
    }

    this._profileForm.setValue({
      name: profile.name,
      description: profile.description,
      domainsType,
      allowedDomains,
      restrictedDomains,
      countriesType,
      allowedCountries,
      restrictedCountries,
      ipsType,
      allowedIps,
      restrictedIps,
      flavorsType,
      allowedFlavors,
      restrictedFlavors,
      secureVideo,
      allowPreview,
      preview
    });

    if (profile.isDefault) {
      this._nameField.disable({ onlySelf: true });
    }
  }
{'fraction_non_alphanumeric': 0.09697007249519797, 'fraction_numerical': 0.00012392341532932647, 'mean_word_length': 4.596393897364771, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6965904', 'n_tokens_mistral': 4769, 'n_tokens_neox': 4618, 'n_words': 859}