<commit_before>import localForage from 'localforage';
import { flatten, map, mapKeys, size } from 'lodash';

import Entities from 'constants/entities';
import { createId, coordinatesFromKey } from 'utils/common';

export const clearLocalDatabase = () => localForage.clear();

export const setEntity = (entity, key) => localForage.setItem(key, entity);

export const setEntities = entities =>
  Promise.all(
    map(
      Object.assign(
        ...flatten(
          map(entities, (list, type) =>
            mapKeys(list, (_, id) => `${type}.${id}`),
          ),
        ),
      ),
      setEntity,
    ),
  );

export const convertOldSectors = oldSectors =>
  Promise.all(
    map(oldSectors, (sector, sectorId) =>
      Promise.all([
        setEntity(
          {
            name: sector.name,
            columns: sector.columns,
            rows: sector.rows,
          },
          `${Entities.sector.key}.${sectorId}`,
        ),
        Promise.all(
          map(sector.systems, (system, systemKey) => {
            const systemId = createId();
            return Promise.all([
              setEntity(
                {
                  name: system.name,
                  parent: sectorId,
                  parentEntity: Entities.sector.key,
                  sector: sectorId,
                  ...coordinatesFromKey(systemKey),
                },
                `${Entities.system.key}.${systemId}`,
              ),
              Promise.all(
                map(system.planets, planet =>
                  Promise.all([
                    setEntity(
                      {
                        name: planet.name,
                        parent: systemId,
                        parentEntity: Entities.system.key,
                        sector: sectorId,
                        attributes: {
                          atmosphere: planet.atmosphere,
                          biosphere: planet.biosphere,
                          population: planet.population,
                          tags: planet.tags,
                          techLevel: planet.techLevel,
                          temperature: planet.temperature,
                        },
                      },
                      `${Entities.planet.key}.${createId()}`,
                    ),
                  ]),
                ),
              ),
            ]);
          }),
        ),
      ]),
    ),
  );

export const getEntities = () =>
  new Promise((resolve, reject) => {
    const entities = {};
    const oldSectors = {};
    localForage
      .iterate((entity, key) => {
        if (key.indexOf('.') < 0) {
          oldSectors[key] = entity;
        } else {
          const [entityType, entityId] = key.split('.');
          entities[entityType] = entities[entityType] || {};
          entities[entityType][entityId] = entity;
        }
      })
      .then(() => {
        if (!size(oldSectors)) {
          return resolve(entities);
        }
        return clearLocalDatabase()
          .then(() => convertOldSectors(oldSectors))
          .then(() => {
            localForage.iterate((entity, key) => {
              const [entityType, entityId] = key.split('.');
              entities[entityType] = entities[entityType] || {};
              entities[entityType][entityId] = entity;
            });
            return resolve(entities);
          });
      })<commit_msg>Ensure converted entities are resolved correctly to the initialization action<commit_after>import localForage from 'localforage';
import { flatten, map, mapKeys, size } from 'lodash';

import Entities from 'constants/entities';
import { createId, coordinatesFromKey } from 'utils/common';

export const clearLocalDatabase = () => localForage.clear();

export const setEntity = (entity, key) => localForage.setItem(key, entity);

export const setEntities = entities =>
  Promise.all(
    map(
      Object.assign(
        ...flatten(
          map(entities, (list, type) =>
            mapKeys(list, (_, id) => `${type}.${id}`),
          ),
        ),
      ),
      setEntity,
    ),
  );

export const convertOldSectors = oldSectors =>
  Promise.all(
    map(oldSectors, (sector, sectorId) =>
      Promise.all([
        setEntity(
          {
            name: sector.name,
            columns: sector.columns,
            rows: sector.rows,
          },
          `${Entities.sector.key}.${sectorId}`,
        ),
        Promise.all(
          map(sector.systems, (system, systemKey) => {
            const systemId = createId();
            return Promise.all([
              setEntity(
                {
                  name: system.name,
                  parent: sectorId,
                  parentEntity: Entities.sector.key,
                  sector: sectorId,
                  ...coordinatesFromKey(systemKey),
                },
                `${Entities.system.key}.${systemId}`,
              ),
              Promise.all(
                map(system.planets, planet =>
                  Promise.all([
                    setEntity(
                      {
                        name: planet.name,
                        parent: systemId,
                        parentEntity: Entities.system.key,
                        sector: sectorId,
                        attributes: {
                          atmosphere: planet.atmosphere,
                          biosphere: planet.biosphere,
                          population: planet.population,
                          tags: planet.tags,
                          techLevel: planet.techLevel,
                          temperature: planet.temperature,
                        },
                      },
                      `${Entities.planet.key}.${createId()}`,
                    ),
                  ]),
                ),
              ),
            ]);
          }),
        ),
      ]),
    ),
  );

export const getEntities = () =>
  new Promise((resolve, reject) => {
    const entities = {};
    const oldSectors = {};
    localForage
      .iterate((entity, key) => {
        if (key.indexOf('.') < 0) {
          oldSectors[key] = entity;
        } else {
          const [entityType, entityId] = key.split('.');
          entities[entityType] = entities[entityType] || {};
          entities[entityType][entityId] = entity;
        }
      })
      .then(() => {
        if (!size(oldSectors)) {
          return resolve(entities);
        }
        return clearLocalDatabase()
          .then(() => convertOldSectors(oldSectors))
          .then(() =>
            localForage
              .iterate((entity, key) => {
                const [entityType, entityId] = key.split('.');
                entities[entityType] = entities[entityType] || {};
                entities[entityType][entityId] = entity;
              })
              .then(() => resolve(entities)),
          );
      })
{'fraction_non_alphanumeric': 0.12516373162567312, 'fraction_numerical': 0.00029107844564110026, 'mean_word_length': 1.3029490616621984, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 36, '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': '7450208', 'n_tokens_mistral': 1770, 'n_tokens_neox': 1739, 'n_words': 328}