<commit_before>import { h } from 'snabbdom';
import { bind, onInsert } from './util';
import { Api as CgApi } from 'chessground/api';
import * as cgUtil from 'chessground/util';
import { Role } from 'chessground/types';
import { MaybeVNode, Vm, Redraw, Promotion } from './interfaces';
import { Prop } from 'common';

export default function (vm: Vm, getGround: Prop<CgApi>, redraw: Redraw): Promotion {
  let promoting: false | { orig: Key; dest: Key; callback: (orig: Key, key: Key, prom: Role) => void } = false;

  function start(orig: Key, dest: Key, callback: (orig: Key, key: Key, prom: Role) => void) {
    const g = getGround(),
      piece = g.state.pieces.get(dest);
    if (
      piece &&
      piece.role == 'pawn' &&
      ((dest[1] == '8' && g.state.turnColor == 'black') || (dest[1] == '1' && g.state.turnColor == 'white'))
    ) {
      promoting = {
        orig,
        dest,
        callback,
      };
      redraw();
      return true;
    }
    return false;
  }

  function promote(g: CgApi, key: Key, role: Role): void {
    const piece = g.state.pieces.get(key);
    if (piece && piece.role == 'pawn') {
      g.setPieces(
        new Map([
          [
            key,
            {
              color: piece.color,
              role,
              promoted: true,
            },
          ],
        ])
      );
    }
  }

  function finish(role: Role): void {
    if (promoting) {
      promote(getGround(), promoting.dest, role);
      promoting?.callback(promoting.orig, promoting.dest, role);
    }
    promoting = false;
  }

  function cancel(): void {
    if (promoting) {
      promoting = false;
      getGround().set(vm.cgConfig);
      redraw();
    }
  }<commit_msg>remove superfluous `?` - for #8720<commit_after>import { h } from 'snabbdom';
import { bind, onInsert } from './util';
import { Api as CgApi } from 'chessground/api';
import * as cgUtil from 'chessground/util';
import { Role } from 'chessground/types';
import { MaybeVNode, Vm, Redraw, Promotion } from './interfaces';
import { Prop } from 'common';

export default function (vm: Vm, getGround: Prop<CgApi>, redraw: Redraw): Promotion {
  let promoting: false | { orig: Key; dest: Key; callback: (orig: Key, key: Key, prom: Role) => void } = false;

  function start(orig: Key, dest: Key, callback: (orig: Key, key: Key, prom: Role) => void) {
    const g = getGround(),
      piece = g.state.pieces.get(dest);
    if (
      piece &&
      piece.role == 'pawn' &&
      ((dest[1] == '8' && g.state.turnColor == 'black') || (dest[1] == '1' && g.state.turnColor == 'white'))
    ) {
      promoting = {
        orig,
        dest,
        callback,
      };
      redraw();
      return true;
    }
    return false;
  }

  function promote(g: CgApi, key: Key, role: Role): void {
    const piece = g.state.pieces.get(key);
    if (piece && piece.role == 'pawn') {
      g.setPieces(
        new Map([
          [
            key,
            {
              color: piece.color,
              role,
              promoted: true,
            },
          ],
        ])
      );
    }
  }

  function finish(role: Role): void {
    if (promoting) {
      promote(getGround(), promoting.dest, role);
      promoting.callback(promoting.orig, promoting.dest, role);
    }
    promoting = false;
  }

  function cancel(): void {
    if (promoting) {
      promoting = false;
      getGround().set(vm.cgConfig);
      redraw();
    }
  }
{'fraction_non_alphanumeric': 0.15706806282722513, 'fraction_numerical': 0.0034904013961605585, 'mean_word_length': 2.1637534498620057, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '6314538', 'n_tokens_mistral': 1126, 'n_tokens_neox': 1128, 'n_words': 297}