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

//#region src/gui/EFFitScale.ts
/**
* Compute the scale factor and centering translation needed to fit
* content of a given size into a container while preserving aspect ratio.
*
* Returns `null` when any dimension is zero or negative (cannot compute).
*/
function computeFitScale(input) {
	const { containerWidth, containerHeight, contentWidth, contentHeight } = input;
	if (containerWidth <= 0 || containerHeight <= 0 || contentWidth <= 0 || contentHeight <= 0) return null;
	const scale = containerWidth / containerHeight > contentWidth / contentHeight ? containerHeight / contentHeight : containerWidth / contentWidth;
	const scaledWidth = contentWidth * scale;
	const scaledHeight = contentHeight * scale;
	return {
		scale,
		translateX: (containerWidth - scaledWidth) / 2,
		translateY: (containerHeight - scaledHeight) / 2
	};
}
let EFFitScale = class EFFitScale$1 extends LitElement {
	constructor(..._args) {
		super(..._args);
		this.containerRef = createRef();
		this.contentRef = createRef();
		this.uniqueId = Math.random().toString(36).substring(2, 15);
		this.paused = false;
		this.observedContentChild = null;
		this.hasWarnedZeroDimensions = false;
		this.scaleLastSetOn = null;
		this.updateScale = () => {
			if (!this.isConnected || this.paused) return;
			const { containerWidth, containerHeight, contentWidth, contentHeight } = this.scaleInfo;
			if (containerWidth === 0 || containerHeight === 0) {
				if (!this.hasWarnedZeroDimensions) {
					this.hasWarnedZeroDimensions = true;
					console.warn(`[ef-fit-scale] Container has zero dimensions (${containerWidth}×${containerHeight}). Content will be invisible. Ensure all ancestors have resolved height.`, this);
				}
				return;
			}
			this.hasWarnedZeroDimensions = false;
			if (this.contentChild && contentWidth > 0 && contentHeight > 0) {
				const result = computeFitScale({
					containerWidth,
					containerHeight,
					contentWidth,
					contentHeight
				});
				if (!result) return;
				if (this.scaleLastSetOn !== this.contentChild) this.removeScale();
				Object.assign(this.contentChild.style, {
					width: `${contentWidth}px`,
					height: `${contentHeight}px`,
					transform: `translate(${result.translateX.toFixed(4)}px, ${result.translateY.toFixed(4)}px) scale(${result.scale.toFixed(4)})`,
					transformOrigin: "top left"
				});
				this.scaleLastSetOn = this.contentChild;
			}
		};
		this.removeScale = () => {
			if (this.scaleLastSetOn) {
				Object.assign(this.scaleLastSetOn.style, {
					width: "",
					height: "",
					transform: "",
					transformOrigin: ""
				});
				this.scaleLastSetOn = null;
			}
		};
	}
	createRenderRoot() {
		Object.assign(this.style, {
			display: "grid",
			width: "100%",
			height: "100%",
			gridTemplateColumns: "100%",
			gridTemplateRows: "100%",
			overflow: "hidden",
			boxSizing: "border-box",
			contain: "layout paint style",
			position: "relative"
		});
		this.id = `${this.uniqueId}`;
		return this;
	}
	updated(changedProperties) {
		if (changedProperties.has("paused") && !this.paused) this.updateScale();
	}
	get contentChild() {
		if (!this.children.length) return null;
		const isNonContentElement = (element) => {
			const tagName = element.tagName.toLowerCase();
			if ([
				"style",
				"script",
				"meta",
				"link",
				"title",
				"noscript"
			].includes(tagName)) return true;
			try {
				const display = window.getComputedStyle(element).display;
				return display === "none" || display === "contents";
			} catch {
				return false;
			}
		};
		const findAllContentElements = (element) => {
			const results = [];
			if (element instanceof HTMLSlotElement) {
				const assigned = element.assignedElements()[0];
				if (assigned) results.push(...findAllContentElements(assigned));
				return results;
			}
			if (!isNonContentElement(element)) results.push(element);
			const children$1 = Array.from(element.children);
			for (let i = 0; i < children$1.length; i++) {
				const child = children$1[i];
				if (child) results.push(...findAllContentElements(child));
			}
			return results;
		};
		const children = Array.from(this.children);
		const allContentElements = [];
		for (let i = 0; i < children.length; i++) {
			const child = children[i];
			if (child) allContentElements.push(...findAllContentElements(child));
		}
		if (allContentElements.length === 0) return null;
		return allContentElements[0] ?? null;
	}
	get scaleInfo() {
		if (!this.contentChild) return {
			scale: 1,
			containerWidth: 0,
			containerHeight: 0,
			contentWidth: 0,
			contentHeight: 0
		};
		const containerWidth = this.clientWidth;
		const containerHeight = this.clientHeight;
		let contentWidth = 0;
		let contentHeight = 0;
		if (typeof this.contentChild.getNaturalDimensions === "function") {
			const naturalDimensions = this.contentChild.getNaturalDimensions();
			if (naturalDimensions && naturalDimensions.width > 0 && naturalDimensions.height > 0) {
				contentWidth = naturalDimensions.width;
				contentHeight = naturalDimensions.height;
				if (this.contentChild.tagName === "EF-VIDEO") {
					const canvas = this.contentChild.canvasElement;
					if (canvas) {
						canvas.style.setProperty("width", `${contentWidth}px`, "important");
						canvas.style.setProperty("height", `${contentHeight}px`, "important");
					}
				}
			} else {
				contentWidth = this.contentChild.clientWidth;
				contentHeight = this.contentChild.clientHeight;
			}
		} else {
			contentWidth = this.contentChild.clientWidth;
			contentHeight = this.contentChild.clientHeight;
		}
		if (contentWidth === 0 || contentHeight === 0) return {
			scale: 1,
			containerWidth,
			containerHeight,
			contentWidth: 0,
			contentHeight: 0
		};
		return {
			scale: computeFitScale({
				containerWidth,
				containerHeight,
				contentWidth,
				contentHeight
			})?.scale ?? 1,
			containerWidth,
			containerHeight,
			contentWidth,
			contentHeight
		};
	}
	observeContentChild() {
		const child = this.contentChild;
		if (child === this.observedContentChild) return;
		this.contentResizeObserver?.disconnect();
		if (child) {
			this.contentResizeObserver = new ResizeObserver(() => {
				this.updateScale();
			});
			this.contentResizeObserver.observe(child);
		}
		this.observedContentChild = child;
	}
	connectedCallback() {
		super.connectedCallback();
		this.hasWarnedZeroDimensions = false;
		this.containerResizeObserver = new ResizeObserver(() => {
			this.updateScale();
		});
		this.containerResizeObserver.observe(this);
		this.childMutationObserver = new MutationObserver(() => {
			this.observeContentChild();
			this.updateScale();
		});
		this.childMutationObserver.observe(this, { childList: true });
		this.observeContentChild();
		requestAnimationFrame(() => this.updateScale());
	}
	disconnectedCallback() {
		super.disconnectedCallback();
		this.removeScale();
		this.containerResizeObserver?.disconnect();
		this.contentResizeObserver?.disconnect();
		this.childMutationObserver?.disconnect();
		this.observedContentChild = null;
	}
};
__decorate([property({ type: Boolean })], EFFitScale.prototype, "paused", void 0);
EFFitScale = __decorate([customElement("ef-fit-scale")], EFFitScale);

//#endregion
export { EFFitScale, computeFitScale };
//# sourceMappingURL=EFFitScale.js.map