<commit_before>import { parse, stringify, StringifiableRecord } from "query-string"
import {
  MaanmittauslaitosLayer,
  MaisemanMuistiLayer,
  ModelLayer,
  MuseovirastoLayer,
  Settings
} from "../types"

const parseURLParams = () => {
  const hash = window.location.hash.replace(";", "&") // Old format used ";" as separator
  return parse(hash, {
    parseNumbers: true,
    parseBooleans: true,
    arrayFormat: "comma"
  })
}

const stringifyURLParamsToHash = (params: StringifiableRecord): string =>
  `#${stringify(params, {
    arrayFormat: "comma"
  })}`

export const createLocationHash = (coordinates: [number, number]) => {
  return stringifyURLParamsToHash({
    ...parseURLParams(), // Keep old params
    x: coordinates[0],
    y: coordinates[1]
  })
}

export const parseCoordinatesFromURL = (): [number, number] | null => {
  const urlParams = parseURLParams()
  const x = parseFloat(String(urlParams.x))
  const y = parseFloat(String(urlParams.y))

  if (!isNaN(x) && !isNaN(y)) {
    return [x, y]
  }
  return null
}

interface URLSettings {
  zoom?: number
  mmlLayer?: string
  museovirastoLayer?: string | Array<string>
  modelsVisible?: boolean
  maisemanMuistiLayerVisible?: boolean
}

const EMPTY_SELECTION = "none"

export const updateSettingsToURL = (
  initialSettings: Settings,
  currentSettings: Settings
) => {
  const urlSettings: URLSettings = {}
  let oldUrlSettings = parseURLParams() as URLSettings

  // MML
  if (
    initialSettings.maanmittauslaitos.selectedLayer ===
    currentSettings.maanmittauslaitos.selectedLayer
  ) {
    oldUrlSettings = {
      ...oldUrlSettings,
      mmlLayer: undefined
    }
  } else {
    urlSettings.mmlLayer = currentSettings.maanmittauslaitos.selectedLayer
  }

  // Museovirasto layer
  if (
    initialSettings.museovirasto.selectedLayers.every((v) =>
      currentSettings.museovirasto.selectedLayers.includes(v)
    )
  ) {
    oldUrlSettings = {
      ...oldUrlSettings,
      museovirastoLayer: undefined
    }
  } else {
    urlSettings.museovirastoLayer =
      currentSettings.museovirasto.selectedLayers.length > 0
        ? currentSettings.museovirasto.selectedLayers
        : [EMPTY_SELECTION]
  }

  // 3D models
  if (
    currentSettings.models.selectedLayers.length ===
    initialSettings.models.selectedLayers.length
  ) {
    oldUrlSettings = {
      ...oldUrlSettings,
      modelsVisible: undefined
    }
  } else {
    urlSettings.modelsVisible = currentSettings.models.selectedLayers.length > 0
  }

  // Maiseman muisti
  if (
    currentSettings.maisemanMuisti.selectedLayers.length ===
    initialSettings.maisemanMuisti.selectedLayers.length
  ) {
    oldUrlSettings = {
      ...oldUrlSettings,
      maisemanMuistiLayerVisible: undefined
    }
  } else {
    urlSettings.maisemanMuistiLayerVisible =
      currentSettings.maisemanMuisti.selectedLayers.length > 0
  }

  window.location.hash = stringifyURLParamsToHash({
    ...oldUrlSettings,
    ...urlSettings
  })
}

export const getSettingsFromURL = (settings: Settings): Settings => {
  let newSettings = settings
  const UrlSettings = parseURLParams() as URLSettings

  // Map initial zoom
  if (UrlSettings.zoom !== undefined) {
    newSettings = {
      ...newSettings,
      initialMapZoom: UrlSettings.zoom
    }
  }

  // MML
  if (
    UrlSettings.mmlLayer &&
    Object.values(MaanmittauslaitosLayer).some(
      (v) => v === UrlSettings.mmlLayer
    )
  ) {
    newSettings = {
      ...newSettings,
      maanmittauslaitos: {
        ...newSettings.maanmittauslaitos,
        selectedLayer: UrlSettings.mmlLayer as MaanmittauslaitosLayer
      }
    }
  }

  // Museovirasto layers
  if (UrlSettings.museovirastoLayer) {
    const allLayers = Object.values(MuseovirastoLayer)
    if (
      typeof UrlSettings.museovirastoLayer === "string" &&
      (allLayers.some((v) => v === UrlSettings.museovirastoLayer) ||
        UrlSettings.museovirastoLayer === EMPTY_SELECTION)
    ) {
      newSettings = {
        ...newSettings,
        museovirasto: {
          ...newSettings.museovirasto,
          selectedLayers:
            UrlSettings.museovirastoLayer === EMPTY_SELECTION
              ? []
              : [UrlSettings.museovirastoLayer as MuseovirastoLayer]
        }
      }
    }
    if (
      Array.isArray(UrlSettings.museovirastoLayer) &&
      UrlSettings.museovirastoLayer.every((v) => allLayers.some((l) => l === v))
    ) {
      newSettings = {
        ...newSettings,
        museovirasto: {
          ...newSettings.museovirasto,
          selectedLayers: UrlSettings.museovirastoLayer as Array<
            MuseovirastoLayer
          >
        }
      }
    }<commit_msg>Add rest of the settings to URL params parsing<commit_after>import { parse, stringify, StringifiableRecord } from "query-string"
import {
  AhvenanmaaLayer,
  MaanmittauslaitosLayer,
  MaisemanMuistiLayer,
  ModelLayer,
  MuinaisjaannosAjoitus,
  MuinaisjaannosTyyppi,
  MuseovirastoLayer,
  Settings
} from "../types"

const parseURLParams = () => {
  const hash = window.location.hash.replace(";", "&") // Old format used ";" as separator
  return parse(hash, {
    parseNumbers: true,
    parseBooleans: true,
    arrayFormat: "comma"
  })
}

const stringifyURLParamsToHash = (params: StringifiableRecord): string =>
  `#${stringify(params, {
    arrayFormat: "comma"
  })}`

export const createLocationHash = (coordinates: [number, number]) => {
  return stringifyURLParamsToHash({
    ...parseURLParams(), // Keep old params
    x: coordinates[0],
    y: coordinates[1]
  })
}

export const parseCoordinatesFromURL = (): [number, number] | null => {
  const urlParams = parseURLParams()
  const x = parseFloat(String(urlParams.x))
  const y = parseFloat(String(urlParams.y))

  if (!isNaN(x) && !isNaN(y)) {
    return [x, y]
  }
  return null
}

interface URLSettings {
  zoom?: number
  mmlLayer?: string
  museovirastoLayer?: string | Array<string>
  muinaisjaannosTypes?: string | Array<string>
  muinaisjaannosDatings?: string | Array<string>
  ahvenanmaaLayer?: string | Array<string>
  modelsVisible?: boolean
  maisemanMuistiLayerVisible?: boolean
}

const EMPTY_SELECTION = "none"

export const updateSettingsToURL = (
  initialSettings: Settings,
  currentSettings: Settings
) => {
  const urlSettings: URLSettings = {}
  let oldUrlSettings = parseURLParams() as URLSettings

  // MML
  if (
    initialSettings.maanmittauslaitos.selectedLayer ===
    currentSettings.maanmittauslaitos.selectedLayer
  ) {
    oldUrlSettings = {
      ...oldUrlSettings,
      mmlLayer: undefined
    }
  } else {
    urlSettings.mmlLayer = currentSettings.maanmittauslaitos.selectedLayer
  }

  // Museovirasto layers
  if (
    initialSettings.museovirasto.selectedLayers.every((v) =>
      currentSettings.museovirasto.selectedLayers.includes(v)
    )
  ) {
    oldUrlSettings = {
      ...oldUrlSettings,
      museovirastoLayer: undefined
    }
  } else {
    urlSettings.museovirastoLayer =
      currentSettings.museovirasto.selectedLayers.length > 0
        ? currentSettings.museovirasto.selectedLayers
        : [EMPTY_SELECTION]
  }

  // Museovirasto muinaisjaannos types
  if (
    initialSettings.museovirasto.selectedMuinaisjaannosTypes.every((v) =>
      currentSettings.museovirasto.selectedMuinaisjaannosTypes.includes(v)
    )
  ) {
    oldUrlSettings = {
      ...oldUrlSettings,
      muinaisjaannosTypes: undefined
    }
  } else {
    urlSettings.muinaisjaannosTypes =
      currentSettings.museovirasto.selectedMuinaisjaannosTypes.length > 0
        ? currentSettings.museovirasto.selectedMuinaisjaannosTypes
        : [EMPTY_SELECTION]
  }

  // Museovirasto muinaisjaannos datings
  if (
    initialSettings.museovirasto.selectedMuinaisjaannosDatings.every((v) =>
      currentSettings.museovirasto.selectedMuinaisjaannosDatings.includes(v)
    )
  ) {
    oldUrlSettings = {
      ...oldUrlSettings,
      muinaisjaannosDatings: undefined
    }
  } else {
    urlSettings.muinaisjaannosDatings =
      currentSettings.museovirasto.selectedMuinaisjaannosDatings.length > 0
        ? currentSettings.museovirasto.selectedMuinaisjaannosDatings
        : [EMPTY_SELECTION]
  }

  // Ahvenanmaa layers
  if (
    initialSettings.ahvenanmaa.selectedLayers.every((v) =>
      currentSettings.ahvenanmaa.selectedLayers.includes(v)
    )
  ) {
    oldUrlSettings = {
      ...oldUrlSettings,
      ahvenanmaaLayer: undefined
    }
  } else {
    urlSettings.ahvenanmaaLayer =
      currentSettings.ahvenanmaa.selectedLayers.length > 0
        ? currentSettings.ahvenanmaa.selectedLayers
        : [EMPTY_SELECTION]
  }

  // 3D models
  if (
    currentSettings.models.selectedLayers.length ===
    initialSettings.models.selectedLayers.length
  ) {
    oldUrlSettings = {
      ...oldUrlSettings,
      modelsVisible: undefined
    }
  } else {
    urlSettings.modelsVisible = currentSettings.models.selectedLayers.length > 0
  }

  // Maiseman muisti
  if (
    currentSettings.maisemanMuisti.selectedLayers.length ===
    initialSettings.maisemanMuisti.selectedLayers.length
  ) {
    oldUrlSettings = {
      ...oldUrlSettings,
      maisemanMuistiLayerVisible: undefined
    }
  } else {
    urlSettings.maisemanMuistiLayerVisible =
      currentSettings.maisemanMuisti.selectedLayers.length > 0
  }

  window.location.hash = stringifyURLParamsToHash({
    ...oldUrlSettings,
    ...urlSettings
  })
}

export const getSettingsFromURL = (settings: Settings): Settings => {
  let newSettings = settings
  const UrlSettings = parseURLParams() as URLSettings

  // Map initial zoom
  if (UrlSettings.zoom !== undefined) {
    newSettings = {
      ...newSettings,
      initialMapZoom: UrlSettings.zoom
    }
  }

  // MML
  if (
    UrlSettings.mmlLayer &&
    Object.values(MaanmittauslaitosLayer).some(
      (v) => v === UrlSettings.mmlLayer
    )
  ) {
    newSettings = {
      ...newSettings,
      maanmittauslaitos: {
        ...newSettings.maanmittauslaitos,
        selectedLayer: UrlSettings.mmlLayer as MaanmittauslaitosLayer
      }
    }
  }

  // Museovirasto layers
  if (UrlSettings.museovirastoLayer) {
    const allLayers = Object.values(MuseovirastoLayer)
    if (
      typeof UrlSettings.museovirastoLayer === "string" &&
      (allLayers.some((v) => v === UrlSettings.museovirastoLayer) ||
        UrlSettings.museovirastoLayer === EMPTY_SELECTION)
    ) {
      newSettings = {
        ...newSettings,
        museovirasto: {
          ...newSettings.museovirasto,
          selectedLayers:
            UrlSettings.museovirastoLayer === EMPTY_SELECTION
              ? []
              : [UrlSettings.museovirastoLayer as MuseovirastoLayer]
        }
      }
    }
    if (
      Array.isArray(UrlSettings.museovirastoLayer) &&
      UrlSettings.museovirastoLayer.every((v) => allLayers.some((l) => l === v))
    ) {
      newSettings = {
        ...newSettings,
        museovirasto: {
          ...newSettings.museovirasto,
          selectedLayers: UrlSettings.museovirastoLayer as Array<
            MuseovirastoLayer
          >
        }
      }
    }
  }

  // Museovirasto muinaisjaannos types
  if (UrlSettings.muinaisjaannosTypes) {
    const allTypes = Object.values(MuinaisjaannosTyyppi)
    if (
      typeof UrlSettings.muinaisjaannosTypes === "string" &&
      (allTypes.some((v) => v === UrlSettings.muinaisjaannosTypes) ||
        UrlSettings.muinaisjaannosTypes === EMPTY_SELECTION)
    ) {
      newSettings = {
        ...newSettings,
        museovirasto: {
          ...newSettings.museovirasto,
          selectedMuinaisjaannosTypes:
            UrlSettings.muinaisjaannosTypes === EMPTY_SELECTION
              ? []
              : [UrlSettings.muinaisjaannosTypes as MuinaisjaannosTyyppi]
        }
      }
    }
    if (
      Array.isArray(UrlSettings.muinaisjaannosTypes) &&
      UrlSettings.muinaisjaannosTypes.every((v) =>
        allTypes.some((l) => l === v)
      )
    ) {
      newSettings = {
        ...newSettings,
        museovirasto: {
          ...newSettings.museovirasto,
          selectedMuinaisjaannosTypes: UrlSettings.muinaisjaannosTypes as Array<
            MuinaisjaannosTyyppi
          >
        }
      }
    }
  }

  // Museovirasto muinaisjaannos datings
  if (UrlSettings.muinaisjaannosDatings) {
    const allDatings = Object.values(MuinaisjaannosAjoitus)
    if (
      typeof UrlSettings.muinaisjaannosDatings === "string" &&
      (allDatings.some((v) => v === UrlSettings.muinaisjaannosDatings) ||
        UrlSettings.muinaisjaannosDatings === EMPTY_SELECTION)
    ) {
      newSettings = {
        ...newSettings,
        museovirasto: {
          ...newSettings.museovirasto,
          selectedMuinaisjaannosDatings:
            UrlSettings.muinaisjaannosDatings === EMPTY_SELECTION
              ? []
              : [UrlSettings.muinaisjaannosDatings as MuinaisjaannosAjoitus]
        }
      }
    }
    if (
      Array.isArray(UrlSettings.muinaisjaannosDatings) &&
      UrlSettings.muinaisjaannosDatings.every((v) =>
        allDatings.some((l) => l === v)
      )
    ) {
      newSettings = {
        ...newSettings,
        museovirasto: {
          ...newSettings.museovirasto,
          selectedMuinaisjaannosDatings: UrlSettings.muinaisjaannosDatings as Array<
            MuinaisjaannosAjoitus
          >
        }
      }
    }
  }

  // Ahvenanmaa layers
  if (UrlSettings.ahvenanmaaLayer) {
    const allLayers = Object.values(AhvenanmaaLayer)
    if (
      typeof UrlSettings.ahvenanmaaLayer === "string" &&
      (allLayers.some((v) => v === UrlSettings.ahvenanmaaLayer) ||
        UrlSettings.ahvenanmaaLayer === EMPTY_SELECTION)
    ) {
      newSettings = {
        ...newSettings,
        ahvenanmaa: {
          ...newSettings.ahvenanmaa,
          selectedLayers:
            UrlSettings.ahvenanmaaLayer === EMPTY_SELECTION
              ? []
              : [UrlSettings.ahvenanmaaLayer as AhvenanmaaLayer]
        }
      }
    }
    if (
      Array.isArray(UrlSettings.ahvenanmaaLayer) &&
      UrlSettings.ahvenanmaaLayer.every((v) => allLayers.some((l) => l === v))
    ) {
      newSettings = {
        ...newSettings,
        ahvenanmaa: {
          ...newSettings.ahvenanmaa,
          selectedLayers: UrlSettings.ahvenanmaaLayer as Array<AhvenanmaaLayer>
        }
      }
    }
{'fraction_non_alphanumeric': 0.10679543704947862, 'fraction_numerical': 0.0010497585555322277, 'mean_word_length': 2.7116883116883117, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 56, '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': '4311119', 'n_tokens_mistral': 4749, 'n_tokens_neox': 4665, 'n_words': 683}