<commit_before>import React from 'react';
import {stores} from 'js/stores';
import permConfig from 'js/components/permissions/permConfig';
import {buildUserUrl} from 'utils';
import {
  ASSET_TYPES,
  MODAL_TYPES,
  QUESTION_TYPES,
  META_QUESTION_TYPES,
  GROUP_TYPES_BEGIN,
  GROUP_TYPES_END,
  SCORE_ROW_TYPE,
  RANK_LEVEL_TYPE,
  ANON_USERNAME,
  PERMISSIONS_CODENAMES,
  ACCESS_TYPES,
  ROOT_URL,
} from 'js/constants';

/**
 * Removes whitespace from tags.
 * NOTE: Behavior should match KpiTaggableManager.add()
 * @param {Array<string>} tags - list of tags.
 * @return {Array<string>} list of cleaned up tags.
 */
export function cleanupTags(tags) {
  return tags.map(function(tag) {
    return tag.trim().replace(/ /g, '-');
  });
}

/**
 * Returns nicer "me" label for your own assets.
 * @param {string} username
 * @returns {string} nice owner username.
 */
export function getAssetOwnerDisplayName(username) {
  if (
    stores.session.currentAccount &&
    stores.session.currentAccount.username &&
    stores.session.currentAccount.username === username
  ) {
    return t('me');
  } else {
    return username;
  }
}

/**
 * @param {Object} asset - BE asset data
 * @returns {string}
 */
export function getOrganizationDisplayString(asset) {
  if (asset.settings.organization) {
    return asset.settings.organization;
  } else {
    return '-';
  }
}

/**
 * @param {Object} asset - BE asset data
 * @param {string} langString - language string (the de facto "id")
 * @returns {number|null} the index of language or null if not found
 */
export function getLanguageIndex(asset, langString) {
  let foundIndex = null;

  if (asset.summary?.languages?.length >= 1) {
    asset.summary.languages.forEach((language, index) => {
      if (language === langString) {
        foundIndex = index;
      }
    });
  }

  return foundIndex;
}

/**
 * @param {Object} asset - BE asset data
 * @returns {string}
 */
export function getLanguagesDisplayString(asset) {
  if (
    asset.summary &&
    asset.summary.languages &&
    asset.summary.languages.length >= 1
  ) {
    return asset.summary.languages.join(', ');
  } else {
    return '-';
  }
}

/**
 * @param {Object} asset - BE asset data
 * @returns {string}
 */
export function getSectorDisplayString(asset) {
  if (asset.settings.sector) {
    return asset.settings.sector.label;
  } else {
    return '-';
  }
}

/**
 * @param {Object} asset - BE asset data
 * @param {boolean} showLongName - shoul display long name
 * @returns {string}
 */
export function getCountryDisplayString(asset, showLongName = false) {
  if (asset.settings.country) {
    return showLongName ? asset.settings.country.label : asset.settings.country.value;
  } else {
    return '-';
  }
}

/**
 * @typedef DisplayNameObj
 * @prop {string} [original] - Name typed in by user.
 * @prop {string} [question] - First question name.
 * @prop {string} [empty] - Set when no other is available.
 * @prop {string} final - original, question or empty name - the one to be displayed.
 */

/**
 * Returns a name to be displayed for asset (especially unnamed ones).
 * @param {Object} asset - BE asset data
 * @returns {DisplayNameObj} object containing final name and all useful data.
 */
export function getAssetDisplayName(asset) {
  const output = {};
  if (asset.name) {
    output.original = asset.name;
  }
  if (asset.summary && asset.summary.labels && asset.summary.labels.length > 0) {
    // for unnamed assets, we try to display first question name
    output.question = asset.summary.labels[0];
  }
  if (!output.original && !output.question) {
    output.empty = t('untitled');
  }
  output.final = output.original || output.question || output.empty;
  return output;
}

/**
 * @param {Object} questionOrChoice - Part of BE asset data
 * @param {number} [translationIndex] - defaults to first (default) language
 * @returns {string} usable name of the question or choice when possible, "Unlabelled" otherwise.
 */
export function getQuestionOrChoiceDisplayName(questionOrChoice, translationIndex = 0) {
  if (questionOrChoice.label && Array.isArray(questionOrChoice.label)) {
    return questionOrChoice.label[translationIndex];
  } else if (questionOrChoice.label && !Array.isArray(questionOrChoice.label)) {
    // in rare cases the label could be a string
    return questionOrChoice.label;
  } else if (questionOrChoice.name) {
    return questionOrChoice.name;
  } else if (questionOrChoice.$autoname) {
    return questionOrChoice.$autoname;
  } else {
    t('Unlabelled');
  }
}

/**
 * @param {Object} asset - BE asset data
 * @returns {boolean}
 */
export function isLibraryAsset(assetType) {
  return (
    assetType === ASSET_TYPES.question.id ||
    assetType === ASSET_TYPES.block.id ||
    assetType === ASSET_TYPES.template.id ||
    assetType === ASSET_TYPES.collection.id
  );
}

/**
 * For getting the icon class name for given asset type.
 * @param {Object} asset - BE asset data
 * @returns {string} k-icon CSS class name
 */
export function getAssetIcon(asset) {
  switch (asset.asset_type) {
    case ASSET_TYPES.template.id:
      if (asset.summary?.lock_any) {
        return 'k-icon k-icon-template-locked';
      } else {
        return 'k-icon k-icon-template';
      }
    case ASSET_TYPES.question.id:
      return 'k-icon k-icon-question';
    case ASSET_TYPES.block.id:
      return 'k-icon k-icon-block';
    case ASSET_TYPES.survey.id:
      if (asset.summary?.lock_any) {
        return 'k-icon k-icon-form-locked';
      } else if (asset.has_deployment && !asset.deployment__active) {
        return 'k-icon k-icon-form-archived';
      } else if (asset.has_deployment) {
        return 'k-icon k-icon-form-deployed';
      } else {
        return 'k-icon k-icon-form-draft';
      }<commit_msg>pr changes<commit_after>import React from 'react';
import {stores} from 'js/stores';
import permConfig from 'js/components/permissions/permConfig';
import {buildUserUrl} from 'utils';
import {
  ASSET_TYPES,
  MODAL_TYPES,
  QUESTION_TYPES,
  META_QUESTION_TYPES,
  GROUP_TYPES_BEGIN,
  GROUP_TYPES_END,
  SCORE_ROW_TYPE,
  RANK_LEVEL_TYPE,
  ANON_USERNAME,
  PERMISSIONS_CODENAMES,
  ACCESS_TYPES,
  ROOT_URL,
} from 'js/constants';

/**
 * Removes whitespace from tags.
 * NOTE: Behavior should match KpiTaggableManager.add()
 * @param {Array<string>} tags - list of tags.
 * @return {Array<string>} list of cleaned up tags.
 */
export function cleanupTags(tags) {
  return tags.map(function(tag) {
    return tag.trim().replace(/ /g, '-');
  });
}

/**
 * Returns nicer "me" label for your own assets.
 * @param {string} username
 * @returns {string} nice owner username.
 */
export function getAssetOwnerDisplayName(username) {
  if (
    stores.session.currentAccount &&
    stores.session.currentAccount.username &&
    stores.session.currentAccount.username === username
  ) {
    return t('me');
  } else {
    return username;
  }
}

/**
 * @param {Object} asset - BE asset data
 * @returns {string}
 */
export function getOrganizationDisplayString(asset) {
  if (asset.settings.organization) {
    return asset.settings.organization;
  } else {
    return '-';
  }
}

/**
 * @param {Object} asset - BE asset data
 * @param {string} langString - language string (the de facto "id")
 * @returns {number|null} the index of language or null if not found
 */
export function getLanguageIndex(asset, langString) {
  let foundIndex = null;

  if (asset.summary?.languages?.length >= 1) {
    asset.summary.languages.forEach((language, index) => {
      if (language === langString) {
        foundIndex = index;
      }
    });
  }

  return foundIndex;
}

/**
 * @param {Object} asset - BE asset data
 * @returns {string}
 */
export function getLanguagesDisplayString(asset) {
  if (
    asset.summary &&
    asset.summary.languages &&
    asset.summary.languages.length >= 1
  ) {
    return asset.summary.languages.join(', ');
  } else {
    return '-';
  }
}

/**
 * @param {Object} asset - BE asset data
 * @returns {string}
 */
export function getSectorDisplayString(asset) {
  if (asset.settings.sector) {
    return asset.settings.sector.label;
  } else {
    return '-';
  }
}

/**
 * @param {Object} asset - BE asset data
 * @param {boolean} showLongName - shoul display long name
 * @returns {string}
 */
export function getCountryDisplayString(asset, showLongName = false) {
  if (asset.settings.country) {
    return showLongName ? asset.settings.country.label : asset.settings.country.value;
  } else {
    return '-';
  }
}

/**
 * @typedef DisplayNameObj
 * @prop {string} [original] - Name typed in by user.
 * @prop {string} [question] - First question name.
 * @prop {string} [empty] - Set when no other is available.
 * @prop {string} final - original, question or empty name - the one to be displayed.
 */

/**
 * Returns a name to be displayed for asset (especially unnamed ones).
 * @param {Object} asset - BE asset data
 * @returns {DisplayNameObj} object containing final name and all useful data.
 */
export function getAssetDisplayName(asset) {
  const output = {};
  if (asset.name) {
    output.original = asset.name;
  }
  if (asset.summary && asset.summary.labels && asset.summary.labels.length > 0) {
    // for unnamed assets, we try to display first question name
    output.question = asset.summary.labels[0];
  }
  if (!output.original && !output.question) {
    output.empty = t('untitled');
  }
  output.final = output.original || output.question || output.empty;
  return output;
}

/**
 * @param {Object} questionOrChoice - Part of BE asset data
 * @param {number} [translationIndex] - defaults to first (default) language
 * @returns {string} usable name of the question or choice when possible, "Unlabelled" otherwise.
 */
export function getQuestionOrChoiceDisplayName(questionOrChoice, translationIndex = 0) {
  if (questionOrChoice.label && Array.isArray(questionOrChoice.label)) {
    return questionOrChoice.label[translationIndex];
  } else if (questionOrChoice.label && !Array.isArray(questionOrChoice.label)) {
    // in rare cases the label could be a string
    return questionOrChoice.label;
  } else if (questionOrChoice.name) {
    return questionOrChoice.name;
  } else if (questionOrChoice.$autoname) {
    return questionOrChoice.$autoname;
  } else {
    t('Unlabelled');
  }
}

/**
 * @param {Object} asset - BE asset data
 * @returns {boolean}
 */
export function isLibraryAsset(assetType) {
  return (
    assetType === ASSET_TYPES.question.id ||
    assetType === ASSET_TYPES.block.id ||
    assetType === ASSET_TYPES.template.id ||
    assetType === ASSET_TYPES.collection.id
  );
}

/**
 * For getting the icon class name for given asset type.
 * @param {Object} asset - BE asset data
 * @returns {string} Contians two class names: Base `k-icon` class name and respective CSS class name
 */
export function getAssetIcon(asset) {
  switch (asset.asset_type) {
    case ASSET_TYPES.template.id:
      if (asset.summary?.lock_any) {
        return 'k-icon k-icon-template-locked';
      } else {
        return 'k-icon k-icon-template';
      }
    case ASSET_TYPES.question.id:
      return 'k-icon k-icon-question';
    case ASSET_TYPES.block.id:
      return 'k-icon k-icon-block';
    case ASSET_TYPES.survey.id:
      if (asset.summary?.lock_any) {
        return 'k-icon k-icon-form-locked';
      } else if (asset.has_deployment && !asset.deployment__active) {
        return 'k-icon k-icon-form-archived';
      } else if (asset.has_deployment) {
        return 'k-icon k-icon-form-deployed';
      } else {
        return 'k-icon k-icon-form-draft';
      }
{'fraction_non_alphanumeric': 0.12984663105290367, 'fraction_numerical': 0.0008616232982939859, 'mean_word_length': 3.3553470919324577, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, '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': '6165117', 'n_tokens_mistral': 3671, 'n_tokens_neox': 3645, 'n_words': 964}