const METERS_PER_PIXEL = 0.05962332392785686;
export function getCurrentInstruction(userLocation, instructions) {
  var _closestInstruction2, _closestInstruction3;
  var closestInstruction = undefined;
  instructions.forEach(function (instruction) {
    const distance = getPerpendicularDistanceFromPath(userLocation, instruction.startPoint, instruction.endPoint);
    const distanceAlongPath = getDistanceAlongPath(userLocation, instruction.startPoint, instruction.endPoint);

    // Ignore matches if not sufficiently within bounds of path
    if (!(distanceAlongPath > -0.5 && distanceAlongPath < 1.5)) {
      return;
    }
    let remainingDistance = getDistanceBetweenPoints(userLocation, instruction.endPoint);
    remainingDistance = Math.round(remainingDistance * METERS_PER_PIXEL * 100) / 100;
    const fullText = generateInstructionText({
      ...instruction,
      remainingDistance
    });
    console.log();
    if (closestInstruction === undefined) {
      closestInstruction = {
        instruction: {
          ...instruction,
          remainingDistance,
          fullText
        },
        distance
      };
      console.log(remainingDistance, userLocation, instruction.endPoint);
    } else {
      if (distance < closestInstruction.distance) {
        closestInstruction = {
          instruction: {
            ...instruction,
            remainingDistance,
            fullText
          },
          distance
        };
        console.log(remainingDistance, userLocation, instruction.endPoint);
      }
    }
  });
  console.log(instructions.findIndex(i => {
    var _closestInstruction;
    return i.wp == ((_closestInstruction = closestInstruction) === null || _closestInstruction === void 0 ? void 0 : _closestInstruction.instruction.wp);
  }));
  // @ts-ignore
  console.log((_closestInstruction2 = closestInstruction) === null || _closestInstruction2 === void 0 ? void 0 : _closestInstruction2.instruction);
  // @ts-ignore
  return (_closestInstruction3 = closestInstruction) === null || _closestInstruction3 === void 0 ? void 0 : _closestInstruction3.instruction;
}
export const preProcessPath = (path, instructions) => {
  var pathSegmentIndex = 0;
  var prevPathIndex = 0;
  var prevInstructionIndex;
  instructions.forEach(function (instruction, instructionIndex) {
    for (var i = pathSegmentIndex; i < path.length; i++) {
      var _path$i;
      const pathPoints = (_path$i = path[i]) === null || _path$i === void 0 || (_path$i = _path$i['_']) === null || _path$i === void 0 ? void 0 : _path$i.points;
      if (pathPoints === undefined) continue;
      for (var j = prevPathIndex; j < pathPoints.length; j++) {
        var _pathPoints$j;
        const point = (_pathPoints$j = pathPoints[j]) === null || _pathPoints$j === void 0 ? void 0 : _pathPoints$j['_'];
        if (point === undefined) continue;
        if (point.id === instruction.wp) {
          instruction.endPoint = point.coordinates;
          if (prevInstructionIndex !== undefined) {
            instruction.startPoint = instructions[prevInstructionIndex].endPoint;
          } else {
            var _pathPoints$;
            instruction.startPoint = (_pathPoints$ = pathPoints[0]) === null || _pathPoints$ === void 0 ? void 0 : _pathPoints$['_'].coordinates;
          }
          prevInstructionIndex = instructionIndex;
          return;
        }
        prevPathIndex = j;
      }
      pathSegmentIndex = i;
      prevPathIndex = 0;
    }
  });
  return instructions;
};

// get distance along path as proportion of total path length
export function getDistanceAlongPath(point, startPathPoint, endPathPoint) {
  const x1 = startPathPoint[0];
  const y1 = startPathPoint[1];
  const x2 = endPathPoint[0];
  const y2 = endPathPoint[1];
  const x0 = point[0];
  const y0 = point[1];
  const numerator = (x2 - x1) * (x0 - x1) + (y2 - y1) * (y0 - y1); // dot product
  const denominator = Math.pow(y2 - y1, 2) + Math.pow(x2 - x1, 2);
  return numerator / denominator;
}

// Get perpendicular distance from point to path
export function getPerpendicularDistanceFromPath(point, startPathPoint, endPathPoint) {
  const x1 = startPathPoint[0];
  const y1 = startPathPoint[1];
  const x2 = endPathPoint[0];
  const y2 = endPathPoint[1];
  const x0 = point[0];
  const y0 = point[1];
  const numerator = Math.abs((y2 - y1) * x0 - (x2 - x1) * y0 + x2 * y1 - y2 * x1);
  const denominator = Math.sqrt(Math.pow(y2 - y1, 2) + Math.pow(x2 - x1, 2));
  return numerator / denominator;
}
export function getDistanceBetweenPoints(point1, point2) {
  const x1 = point1[0];
  const y1 = point1[1];
  const x2 = point2[0];
  const y2 = point2[1];
  return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
}
export function generateInstructionText(instruction) {
  if (instruction.destinationName) return `${instruction.prepend}${instruction.direction} at ${instruction.destinationName} in ${instruction.remainingDistance} meters`;else return `${instruction.prepend}${instruction.direction} in ${instruction.remainingDistance} meters`;
}
//# sourceMappingURL=Instructions.js.map