import { __decorate } from "../_virtual/_@oxc-project_runtime@0.95.0/helpers/decorate.js";
import { TargetController } from "./TargetController.js";
import { LitElement, css, html } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { createRef, ref } from "lit/directives/ref.js";

//#region src/elements/EFSurface.ts
let EFSurface = class EFSurface$1 extends LitElement {
	constructor(..._args) {
		super(..._args);
		this.canvasRef = createRef();
		this.targetElement = null;
		this.target = "";
	}
	static {
		this.styles = [css`
      :host {
        display: block;
        position: relative;
      }
      canvas {
        all: inherit;
        width: 100%;
        height: 100%;
        display: block;
      }
    `];
	}
	#targetController = new TargetController(this);
	render() {
		return html`<canvas ${ref(this.canvasRef)}></canvas>`;
	}
	get rootTimegroup() {
		const target = this.targetElement;
		if (target && "rootTimegroup" in target) return target.rootTimegroup;
		let root = this.closest("ef-timegroup");
		while (root?.parentTimegroup) root = root.parentTimegroup;
		return root;
	}
	get currentTimeMs() {
		return this.rootTimegroup?.currentTimeMs ?? 0;
	}
	get durationMs() {
		return this.rootTimegroup?.durationMs ?? 0;
	}
	get startTimeMs() {
		return this.rootTimegroup?.startTimeMs ?? 0;
	}
	get endTimeMs() {
		return this.startTimeMs + this.durationMs;
	}
	/**
	* Query readiness state for a given time.
	* @implements FrameRenderable
	*/
	getFrameState(_timeMs) {
		return {
			needsPreparation: false,
			isReady: !!this.targetElement,
			priority: 10
		};
	}
	/**
	* Async preparation - no preparation needed.
	* FrameController's priority system ensures dependencies render first.
	* @implements FrameRenderable
	*/
	async prepareFrame(_timeMs, _signal) {}
	/**
	* Synchronous render - copies canvas from target element.
	* @implements FrameRenderable
	*/
	renderFrame(_timeMs) {
		if (this.targetElement) this.copyFromTarget(this.targetElement);
	}
	updated() {
		if (this.targetElement) this.copyFromTarget(this.targetElement);
	}
	getSourceCanvas(from) {
		const anyEl = from;
		if ("canvasElement" in anyEl) return anyEl.canvasElement ?? null;
		const sr = from.shadowRoot;
		if (sr) return sr.querySelector("canvas") ?? null;
		return null;
	}
	copyFromTarget(target) {
		const dst = this.canvasRef.value;
		const src = this.getSourceCanvas(target);
		if (!dst || !src) return;
		if (!src.width || !src.height) return;
		if (dst.width !== src.width || dst.height !== src.height) {
			dst.width = src.width;
			dst.height = src.height;
		}
		const ctx = dst.getContext("2d", { willReadFrequently: true });
		if (!ctx) return;
		ctx.drawImage(src, 0, 0, dst.width, dst.height);
	}
};
__decorate([state()], EFSurface.prototype, "targetElement", void 0);
__decorate([property({ type: String })], EFSurface.prototype, "target", void 0);
EFSurface = __decorate([customElement("ef-surface")], EFSurface);

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