import { LitElement } from "lit";
import { customElement, state } from "lit/decorators.js";
import _decorate from "@oxc-project/runtime/helpers/decorate";
import { createRef } from "lit/directives/ref.js";
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.scale = 1;
		this.scaleLastSetOn = null;
		this.setScale = () => {
			if (this.isConnected) {
				const { scale } = this.scaleInfo;
				if (this.contentChild) {
					const containerRect = this.getBoundingClientRect();
					const contentRect = this.contentChild.getBoundingClientRect();
					const unscaledWidth = contentRect.width / this.scale;
					const unscaledHeight = contentRect.height / this.scale;
					const scaledWidth = unscaledWidth * scale;
					const scaledHeight = unscaledHeight * scale;
					const translateX = (containerRect.width - scaledWidth) / 2;
					const translateY = (containerRect.height - scaledHeight) / 2;
					if (this.scaleLastSetOn !== this.contentChild) this.removeScale();
					Object.assign(this.contentChild.style, {
						transform: `translate(${translateX.toFixed(4)}px, ${translateY.toFixed(4)}px) scale(${scale.toFixed(4)})`,
						transformOrigin: "top left"
					});
					this.scale = scale;
					this.scaleLastSetOn = this.contentChild;
				}
				this.animationFrameId = requestAnimationFrame(this.setScale);
			}
		};
		this.removeScale = () => {
			if (this.scaleLastSetOn) {
				Object.assign(this.scaleLastSetOn.style, {
					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: "strict",
			position: "relative"
		});
		this.id = `${this.uniqueId}`;
		return this;
	}
	get contentChild() {
		const firstElement = this.children[0];
		if (!firstElement) return null;
		let current = firstElement;
		while (current) {
			if (current instanceof HTMLSlotElement) {
				const assigned = current.assignedElements()[0];
				if (!assigned) break;
				current = assigned;
				continue;
			}
			const display = window.getComputedStyle(current).display;
			if (display !== "contents" && display !== "none") return current;
			const firstChild = current.children[0];
			if (!firstChild) break;
			current = firstChild;
		}
		return firstElement;
	}
	get scaleInfo() {
		if (!this.contentChild) return {
			scale: 1,
			containerWidth: 0,
			containerHeight: 0,
			contentWidth: 0,
			contentHeight: 0
		};
		const containerWidth = this.clientWidth;
		const containerHeight = this.clientHeight;
		const contentWidth = this.contentChild.clientWidth;
		const contentHeight = this.contentChild.clientHeight;
		const containerRatio = containerWidth / containerHeight;
		const contentRatio = contentWidth / contentHeight;
		const scale = containerRatio > contentRatio ? containerHeight / contentHeight : containerWidth / contentWidth;
		return {
			scale,
			containerWidth,
			containerHeight,
			contentWidth,
			contentHeight
		};
	}
	connectedCallback() {
		super.connectedCallback();
		this.animationFrameId = requestAnimationFrame(this.setScale);
	}
	disconnectedCallback() {
		super.disconnectedCallback();
		this.removeScale();
		if (this.animationFrameId) cancelAnimationFrame(this.animationFrameId);
	}
};
_decorate([state()], EFFitScale.prototype, "scale", void 0);
EFFitScale = _decorate([customElement("ef-fit-scale")], EFFitScale);
export { EFFitScale };
