/*:
 * @target MZ
 * @plugindesc Text2Frameでデータ作る時のサポート用プラグイン
 * @author
 *
 * @help CBR_makingにデータ入れる
 *
 */

"use strict";
function loadGameDataJson(dirPath, filenameWithExt) {
	const promise = new Promise((resolve, reject) => {
		const url = `${dirPath}${filenameWithExt}`;
		const xhr = new XMLHttpRequest();
		xhr.open("GET", url);
		xhr.overrideMimeType("application/json");
		xhr.onload = () => {
			if (xhr.status < 400) {
				resolve(xhr.responseText);
			} else {
				reject();
			}
		};
		xhr.onerror = reject;
		xhr.send();
	});
	return promise;
}
function loadMapData(dirPath, mapId) {
	const filename = `Map${String(mapId).padStart(3, "0")}`;
	const filenameWithExt = `${filename}.json`;
	return loadGameDataJson(dirPath, filenameWithExt).then((text) => {
		const map = JSON.parse(text);

		CBR_making.map.push({
			data: map,
			mapId: mapId,
			filename: filename
		});
	});
}
function loadAllMapFile(dirPath, mapInfos) {
	const result = [];
	for (const id in mapInfos) {
		if (mapInfos[id]) {
			result.push(loadMapData(dirPath, id));
		}
	}
	return result;
}
function loadAllMapFileEx(dirPath, mapInfos, callback) {
	const promiseList = loadAllMapFile(dirPath, mapInfos);
	Promise.all(promiseList).then(callback);
}

var CBR_making = {
	map: [{}]
};

function actLoadAllMapFile() {
	if ($dataMapInfos) {
		console.log("CBR_making.mapのロード完了");
		loadAllMapFile("data/", $dataMapInfos);
	} else {
		console.log("CBR_making.mapまだロードできない");
		setTimeout(actLoadAllMapFile, 500);
	}
}

(function () {
	setTimeout(actLoadAllMapFile, 0);
})();
