import { VFileMessage } from "../../vfile-message/lib/index.js";
import { minpath } from "./minpath.browser.js";
import { minproc } from "./minproc.browser.js";
import { isUrl } from "./minurl.shared.js";
import { urlToPath } from "./minurl.browser.js";
var order = [
	"history",
	"path",
	"basename",
	"stem",
	"extname",
	"dirname"
], VFile = class {
	constructor(e) {
		let o;
		o = e ? isUrl(e) ? { path: e } : typeof e == "string" || isUint8Array(e) ? { value: e } : e : {}, this.cwd = "cwd" in o ? "" : minproc.cwd(), this.data = {}, this.history = [], this.messages = [], this.value, this.map, this.result, this.stored;
		let l = -1;
		for (; ++l < order.length;) {
			let e = order[l];
			e in o && o[e] !== void 0 && o[e] !== null && (this[e] = e === "history" ? [...o[e]] : o[e]);
		}
		let u;
		for (u in o) order.includes(u) || (this[u] = o[u]);
	}
	get basename() {
		return typeof this.path == "string" ? minpath.basename(this.path) : void 0;
	}
	set basename(e) {
		assertNonEmpty(e, "basename"), assertPart(e, "basename"), this.path = minpath.join(this.dirname || "", e);
	}
	get dirname() {
		return typeof this.path == "string" ? minpath.dirname(this.path) : void 0;
	}
	set dirname(e) {
		assertPath(this.basename, "dirname"), this.path = minpath.join(e || "", this.basename);
	}
	get extname() {
		return typeof this.path == "string" ? minpath.extname(this.path) : void 0;
	}
	set extname(e) {
		if (assertPart(e, "extname"), assertPath(this.dirname, "extname"), e) {
			if (e.codePointAt(0) !== 46) throw Error("`extname` must start with `.`");
			if (e.includes(".", 1)) throw Error("`extname` cannot contain multiple dots");
		}
		this.path = minpath.join(this.dirname, this.stem + (e || ""));
	}
	get path() {
		return this.history[this.history.length - 1];
	}
	set path(e) {
		isUrl(e) && (e = urlToPath(e)), assertNonEmpty(e, "path"), this.path !== e && this.history.push(e);
	}
	get stem() {
		return typeof this.path == "string" ? minpath.basename(this.path, this.extname) : void 0;
	}
	set stem(e) {
		assertNonEmpty(e, "stem"), assertPart(e, "stem"), this.path = minpath.join(this.dirname || "", e + (this.extname || ""));
	}
	fail(e, o, s) {
		let c = this.message(e, o, s);
		throw c.fatal = !0, c;
	}
	info(e, o, s) {
		let c = this.message(e, o, s);
		return c.fatal = void 0, c;
	}
	message(o, s, c) {
		let l = new VFileMessage(o, s, c);
		return this.path && (l.name = this.path + ":" + l.name, l.file = this.path), l.fatal = !1, this.messages.push(l), l;
	}
	toString(e) {
		return this.value === void 0 ? "" : typeof this.value == "string" ? this.value : new TextDecoder(e || void 0).decode(this.value);
	}
};
function assertPart(e, s) {
	if (e && e.includes(minpath.sep)) throw Error("`" + s + "` cannot be a path: did not expect `" + minpath.sep + "`");
}
function assertNonEmpty(e, o) {
	if (!e) throw Error("`" + o + "` cannot be empty");
}
function assertPath(e, o) {
	if (!e) throw Error("Setting `" + o + "` requires `path` to be set too");
}
function isUint8Array(e) {
	return !!(e && typeof e == "object" && "byteLength" in e && "byteOffset" in e);
}
export { VFile };
