import { includes } from 'lodash/collection';

const pieces = {
  1: {
    name: 'pawn',
    imgPath: 'pawn',
  },
  2: {
    name: 'knight',
    imgPath: 'pawn',
  },
  3: {
    name: 'bishop',
    imgPath: 'pawn',
  },
  4: {
    name: 'rook',
    imgPath: 'pawn',
  },
  5: {
    name: 'queen',
    imgPath: 'pawn',
  },
  6: {
    name: 'king',
    imgPath: 'pawn',
  },
};

const getPieceInfoById = (id) => {
  let prefix = Object.hasOwnProperty.call(pieces, id) ? 'white_' : null;
  prefix = Object.hasOwnProperty.call(pieces, Math.abs(id)) ? 'black_' : prefix;
  if (!prefix) throw new Error('Invalid piece id');

  return {
    name: prefix + pieces[id].name,
    imgPath: prefix + pieces[id].imgPath,
  };
};

const isPiece = (id) => (
  includes([1, 2, 3, 4, 5, 6], Math.abs(id))
);

export {
  isPiece,
  getPieceInfoById,
};

export default getPieceInfoById;

@@ -469,12 +469,8 @@
 ? 'w
-hite
 _' :
@@ -478,32 +478,43 @@
 null;%0A  prefix =
+ !prefix &&
  Object.hasOwnPr
@@ -555,12 +555,8 @@
 ? 'b
-lack
 _' :
@@ -654,18 +654,28 @@
  pieces%5B
+Math.abs(
 id
+)
 %5D.name,%0A
@@ -703,18 +703,28 @@
  pieces%5B
+Math.abs(
 id
+)
 %5D.imgPat

Fix piece name bug