import { __decorate } from "../_virtual/_@oxc-project_runtime@0.95.0/helpers/decorate.js";
import { property } from "lit/decorators/property.js";

//#region src/elements/EFSourceMixin.ts
function EFSourceMixin(superClass, options) {
	class EFSourceElement extends superClass {
		constructor(..._args) {
			super(..._args);
			this.src = "";
			this.md5SumLoader = new Proxy({
				run: () => this.loadMd5Sum(),
				host: this
			}, { get(target, prop) {
				if (prop === "value") return target.host._getMd5Value();
				if (prop === "taskComplete") return target.host._getMd5Promise() || Promise.resolve(target.host._getMd5Value());
				return target[prop];
			} });
		}
		get apiHost() {
			return this.closest("ef-configuration")?.apiHost || this.closest("ef-workbench")?.apiHost || this.closest("ef-preview")?.apiHost || window.location.origin;
		}
		#md5Value = void 0;
		#md5Promise = null;
		#md5LastSrc = null;
		productionSrc() {
			if (!this.#md5Value) throw new Error(`MD5 sum not available for ${this}. Cannot generate production URL`);
			if (!this.apiHost) throw new Error(`apiHost not available for ${this}. Cannot generate production URL`);
			return `${this.apiHost}/api/v1/${options.assetType}/${this.#md5Value}`;
		}
		/**
		* Load MD5 sum for the current source
		*/
		async loadMd5Sum(signal) {
			if (this.#md5LastSrc === this.src && this.#md5Value) return this.#md5Value;
			if (this.#md5Promise && this.#md5LastSrc === this.src) return this.#md5Promise;
			this.#md5LastSrc = this.src;
			this.#md5Promise = this.#doLoadMd5(this.src, signal);
			try {
				this.#md5Value = await this.#md5Promise;
				return this.#md5Value;
			} catch (error) {
				if (error instanceof DOMException && error.name === "AbortError") throw error;
				console.error("EFSourceMixin md5Sum error", error);
				return;
			} finally {
				this.#md5Promise = null;
			}
		}
		async #doLoadMd5(src, signal) {
			let normalizedSrc = src.startsWith("/") ? src.slice(1) : src;
			normalizedSrc = normalizedSrc.replace(/^\/+/, "");
			const md5Path = `/api/v1/files/md5?src=${encodeURIComponent(normalizedSrc)}`;
			const response = await fetch(md5Path, { signal });
			if (!response.ok) return;
			return (await response.json()).md5 ?? void 0;
		}
		/** @internal Exposes md5 state for md5SumLoader proxy without private field access in handler */
		_getMd5Value() {
			return this.#md5Value;
		}
		/** @internal Exposes md5 promise state for md5SumLoader proxy */
		_getMd5Promise() {
			return this.#md5Promise;
		}
	}
	__decorate([property({
		type: String,
		reflect: true
	})], EFSourceElement.prototype, "src", void 0);
	return EFSourceElement;
}

//#endregion
export { EFSourceMixin };
//# sourceMappingURL=EFSourceMixin.js.map