/* =========================
   Scripts module
========================= */

export class ScriptsApi {
	constructor(core) {
		this.core = core;
	}

	// GET /api/v1/scripts/actions/list
	actions_list(cb) {
		return this.core.api_get("/api/v1/scripts/actions/list", (err, resp, meta) => {
			if(resp && resp.status && resp.data && resp.data.scripts) {
				for(let item of resp.data.scripts) {
					const id = item?.id ?? item.alias;
					DataProvider.setRaw(`actions_scripts.${id}`, item);
				}
			}

			return cb(err, resp, meta);
		});
	}

	// GET /api/v1/scripts/scopes/list
	scopes_list(cb) {
		return this.core.api_get("/api/v1/scripts/scopes/list", (err, resp, meta) => {
			if(resp && resp.status && resp.data && resp.data.scopes) {
				for(let item of resp.data.scopes) {
					DataProvider.setRaw(`scopes.${item.name}`, item);
				}
			}

			return cb(err, resp, meta);
		});
	}

	// GET /api/v1/scripts/regular/list
	regular_list(cb) {
		return this.core.api_get("/api/v1/scripts/regular/list", (err, resp, meta) => {
			if(resp && resp.status && resp.data && resp.data.scripts) {
				for(let item of resp.data.scripts) {
					const id = item?.id ?? item.alias;
					DataProvider.setRaw(`regular_scripts.${id}`, item);
				}
			}

			return cb(err, resp, meta);
		});
	}

	// GET /api/v1/scripts/scopes/name/{{filename}}
	scope_get_by_filename(filename, cb) {
		const safe = encodeURIComponent(String(filename || ""));
		return this.core.api_get(`/api/v1/scripts/scopes/name/${safe}`, cb, {
			// тут сервер может вернуть PHP-код текстом; request умеет это пережить
		});
	}

	// POST /api/v1/scripts/scopes/new
	scope_create(payload, cb) {
		// payload: { alias, filename, path }
		return this.core.api_post("/api/v1/scripts/scopes/new", payload, cb);
	}

	// POST /api/v1/scripts/scopes/update
	scope_update(payload, cb) {
		// payload: { name, filename, path }
		return this.core.api_post("/api/v1/scripts/scopes/update", payload, cb);
	}

	// GET /api/v1/scripts/actions/alias/{{alias}}/enable
	action_enable(alias, cb) {
		const safe = encodeURIComponent(String(alias || ""));
		return this.core.api_get(`/api/v1/scripts/actions/alias/${safe}/enable`, cb);
	}

	// GET /api/v1/scripts/actions/alias/{{alias}}/disable
	action_disable(alias, cb) {
		const safe = encodeURIComponent(String(alias || ""));
		return this.core.api_get(`/api/v1/scripts/actions/alias/${safe}/disable`, cb);
	}

	// GET /api/v1/scripts/regular/alias/{{alias}}/enable
	regular_enable(alias, cb) {
		const safe = encodeURIComponent(String(alias || ""));
		return this.core.api_get(`/api/v1/scripts/regular/alias/${safe}/enable`, cb);
	}

	// GET /api/v1/scripts/regular/alias/{{alias}}/disable
	regular_disable(alias, cb) {
		const safe = encodeURIComponent(String(alias || ""));
		return this.core.api_get(`/api/v1/scripts/regular/alias/${safe}/disable`, cb);
	}

	// GET /api/v1/scripts/scope/name/{{name}}/enable
	scope_enable(name, cb) {
		const safe = encodeURIComponent(String(name || ""));
		return this.core.api_get(`/api/v1/scripts/scopes/name/${safe}/enable`, cb);
	}

	// GET /api/v1/scripts/actions/scope/name/{{name}}/disable
	scope_disable(name, cb) {
		const safe = encodeURIComponent(String(name || ""));
		return this.core.api_get(`/api/v1/scripts/scopes/name/${safe}/disable`, cb);
	}

	// GET /api/v1/scripts/scopes/name/{{name}}/remove
	scope_remove(name, cb) {
		const safe = encodeURIComponent(String(name || ""));
		return this.core.api_get(`/api/v1/scripts/scopes/name/${safe}/remove`, cb);
	}

	// POST /api/v1/scripts/actions/run
	run(payload, cb) {
		// payload: { alias, params: {...} }
		return this.core.api_post("/api/v1/scripts/actions/run", payload, cb);
	}

	// POST /api/v1/scripts/place-in-area
	place_in_area(payload, cb) {
		// payload: { target_id, place_in_area_id }
		return this.core.api_post("/api/v1/scripts/place-in-area", payload, cb);
	}

	// GET /api/v1/scripts/id/{{target_id}}/unassign-from-area
	unassign_from_area(target_id, cb) {
		const safe = encodeURIComponent(String(target_id));
		return this.core.api_get(`/api/v1/scripts/id/${safe}/unassign-from-area`, cb);
	}
}