<commit_before>import PropTypes from 'prop-types';
import { Component } from 'react';
import { AtomicBlockUtils, Modifier, RichUtils, EditorState } from 'draft-js';
import { ENTITY_TYPE } from 'draftail';

import { STRINGS } from '../../../config/wagtailConfig';
import { getSelectionText } from '../DraftUtils';

const $ = global.jQuery;

const EMBED = 'EMBED';
const DOCUMENT = 'DOCUMENT';

const MUTABILITY = {};
MUTABILITY[ENTITY_TYPE.LINK] = 'MUTABLE';
MUTABILITY[DOCUMENT] = 'MUTABLE';
MUTABILITY[ENTITY_TYPE.IMAGE] = 'IMMUTABLE';
MUTABILITY[EMBED] = 'IMMUTABLE';

export const getChooserConfig = (entityType, entity, selectedText) => {
  const chooserURL = {};
  chooserURL[ENTITY_TYPE.IMAGE] = `${global.chooserUrls.imageChooser}?select_format=true`;
  chooserURL[EMBED] = global.chooserUrls.embedsChooser;
  chooserURL[ENTITY_TYPE.LINK] = global.chooserUrls.pageChooser;
  chooserURL[DOCUMENT] = global.chooserUrls.documentChooser;

  let url = chooserURL[entityType.type];
  let urlParams = {};

  if (entityType.type === ENTITY_TYPE.LINK) {
    urlParams = {
      page_type: 'wagtailcore.page',
      allow_external_link: true,
      allow_email_link: true,
      can_choose_root: 'false',
      link_text: selectedText,
    };

    if (entity) {
      const data = entity.getData();

      if (data.id) {
        url = `${global.chooserUrls.pageChooser}${data.parentId}/`;
      } else if (data.url.startsWith('mailto:')) {
        url = global.chooserUrls.emailLinkChooser;
        urlParams.link_url = data.url.replace('mailto:', '');
      } else {
        url = global.chooserUrls.externalLinkChooser;
        urlParams.link_url = data.url;
      }
    }
  }

  return {
    url,
    urlParams,
  };
};

export const filterEntityData = (entityType, data) => {
  switch (entityType.type) {
  case ENTITY_TYPE.IMAGE:
    return {
      id: data.id,
      src: data.preview.url,
      alt: data.alt,
      format: data.format,
    };
  case EMBED:
    return {
      embedType: data.embedType,
      url: data.url,
      providerName: data.providerName,
      authorName: data.authorName,
      thumbnail: data.thumbnail,
      title: data.title,
    };
  case ENTITY_TYPE.LINK:
    if (data.id) {
      return {
        url: data.url,<commit_msg>Reorganise getChooserConfig for clarity<commit_after>import PropTypes from 'prop-types';
import { Component } from 'react';
import { AtomicBlockUtils, Modifier, RichUtils, EditorState } from 'draft-js';
import { ENTITY_TYPE } from 'draftail';

import { STRINGS } from '../../../config/wagtailConfig';
import { getSelectionText } from '../DraftUtils';

const $ = global.jQuery;

const EMBED = 'EMBED';
const DOCUMENT = 'DOCUMENT';

const MUTABILITY = {};
MUTABILITY[ENTITY_TYPE.LINK] = 'MUTABLE';
MUTABILITY[DOCUMENT] = 'MUTABLE';
MUTABILITY[ENTITY_TYPE.IMAGE] = 'IMMUTABLE';
MUTABILITY[EMBED] = 'IMMUTABLE';

export const getChooserConfig = (entityType, entity, selectedText) => {
  let url;
  let urlParams;

  switch (entityType.type) {
  case ENTITY_TYPE.IMAGE:
    return {
      url: `${global.chooserUrls.imageChooser}?select_format=true`,
      urlParams: {},
    };

  case EMBED:
    return {
      url: global.chooserUrls.embedsChooser,
      urlParams: {},
    };

  case ENTITY_TYPE.LINK:
    url = global.chooserUrls.pageChooser;
    urlParams = {
      page_type: 'wagtailcore.page',
      allow_external_link: true,
      allow_email_link: true,
      can_choose_root: 'false',
      link_text: selectedText,
    };

    if (entity) {
      const data = entity.getData();

      if (data.id) {
        url = `${global.chooserUrls.pageChooser}${data.parentId}/`;
      } else if (data.url.startsWith('mailto:')) {
        url = global.chooserUrls.emailLinkChooser;
        urlParams.link_url = data.url.replace('mailto:', '');
      } else {
        url = global.chooserUrls.externalLinkChooser;
        urlParams.link_url = data.url;
      }
    }

    return {
      url,
      urlParams,
    };

  case DOCUMENT:
    return {
      url: global.chooserUrls.documentChooser,
      urlParams: {},
    };

  default:
    return {
      url: null,
      urlParams: {},
    };
  }
};

export const filterEntityData = (entityType, data) => {
  switch (entityType.type) {
  case ENTITY_TYPE.IMAGE:
    return {
      id: data.id,
      src: data.preview.url,
      alt: data.alt,
      format: data.format,
    };
  case EMBED:
    return {
      embedType: data.embedType,
      url: data.url,
      providerName: data.providerName,
      authorName: data.authorName,
      thumbnail: data.thumbnail,
      title: data.title,
    };
  case ENTITY_TYPE.LINK:
    if (data.id) {
      return {
        url: data.url,
{'fraction_non_alphanumeric': 0.1336179295624333, 'fraction_numerical': 0.0, 'mean_word_length': 2.967823878069433, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '3631570', 'n_tokens_mistral': 1636, 'n_tokens_neox': 1603, 'n_words': 305}