deleteCell method

Future<CatalogRow> deleteCell({
  1. required String keyPath,
  2. required String locale,
})

Implementation

Future<CatalogRow> deleteCell({
  required String keyPath,
  required String locale,
}) async {
  final loaded = await _loadAndSyncState();
  if (!loaded.locales.contains(locale)) {
    throw CatalogOperationException('Locale "$locale" is not configured.');
  }
  if (!loaded.keyPaths.contains(keyPath)) {
    throw CatalogOperationException('Key "$keyPath" does not exist.');
  }

  final localeMap = loaded.dataset.translationsByLocale[locale]!;
  final removed = catalogRemoveValueByPath(localeMap, keyPath);
  if (!removed) {
    throw CatalogOperationException('Key "$keyPath" does not exist in locale "$locale".');
  }

  final now = DateTime.now().toUtc();
  final sourceLocale = config.effectiveSourceLocale;
  final sourceMap = loaded.dataset.translationsByLocale[sourceLocale] ?? const <String, dynamic>{};
  final sourceValue = catalogGetValueByPath(sourceMap, keyPath);

  final keyState = loaded.state.keys.putIfAbsent(
    keyPath,
    () => CatalogKeyState(sourceHash: '', cells: <String, CatalogCellState>{}),
  );

  if (locale == sourceLocale) {
    keyState.sourceHash = '';
    keyState.cells[sourceLocale] = _statusEngine.markWarning(
      current: keyState.cells[sourceLocale],
      reason: CatalogStatusReasons.sourceDeleted,
      now: now,
    );
    for (final targetLocale in loaded.locales) {
      if (targetLocale == sourceLocale) {
        continue;
      }
      keyState.cells[targetLocale] = _statusEngine.markWarning(
        current: keyState.cells[targetLocale],
        reason: CatalogStatusReasons.sourceDeletedReviewRequired,
        now: now,
      );
    }
  } else {
    keyState.cells[locale] = _statusEngine.markRed(
      current: keyState.cells[locale],
      reason:
          sourceValue == null ? CatalogStatusReasons.sourceDeletedReviewRequired : CatalogStatusReasons.targetMissing,
      now: now,
    );
  }
  _appendActivityEvent(
    keyState: keyState,
    event: CatalogActivityEvent(
      kind: CatalogActivityKinds.valueDeleted,
      timestamp: now,
      locale: locale,
    ),
  );

  _mergeDataTypesIntoDataset(loaded.dataset, loaded.state, loaded.locales);
  await _repository.save(loaded.dataset);
  await _stateStore.save(
    config: config,
    projectRootPath: projectRootPath,
    state: loaded.state,
  );

  return _buildRowForKey(
    keyPath: keyPath,
    locales: loaded.locales,
    translationsByLocale: loaded.dataset.translationsByLocale,
    state: loaded.state,
    now: now,
  );
}