"use strict";

const path = require("path");

let platformDoubleSlashMatcher;

if (path.sep === "\\") {
	/* Special case; even within a regex literal, a backslash still needs to be escaped itself. */
	platformDoubleSlashMatcher = /\\+/g;
} else {
	/* See: https://stackoverflow.com/questions/4547609/how-do-you-get-a-string-to-a-character-array-in-javascript/34717402#34717402 */
	let escapedSeparator = Array.from(path.sep).map((character) => `\\${character}`).join("");

	platformDoubleSlashMatcher = new RegExp(`(${escapedSeparator})+`, "g");
}

module.exports = function splitPath(targetPath, normalize = true) {
	let normalized;

	if (normalize) {
		normalized = path.normalize(targetPath);
	} else {
		normalized = targetPath.replace(platformDoubleSlashMatcher, path.sep);
	}

	return normalized.split(path.sep);
};
