import * as Device from "expo-device";
import * as ScreenOrientation from "expo-screen-orientation";
import { useEffect } from "react";

const LANDSCAPE_MODELS = ["ET45", "ET40"]; // tablets
const PORTRAIT_MODELS = ["TC21", "TC26", "TC8300"]; // scanners

const isTabletModel = (modelName?: string | null) => {
	const model = modelName?.toUpperCase() ?? "";

	return LANDSCAPE_MODELS.some((m) => model.includes(m));
};

export function useDeviceOrientationLock() {
	useEffect(() => {
		async function lockOrientation() {
			try {
				const model = Device.modelName;

				await ScreenOrientation.lockAsync(
					isTabletModel(model)
						? ScreenOrientation.OrientationLock.LANDSCAPE
						: ScreenOrientation.OrientationLock.PORTRAIT_UP,
				);
			} catch (err) {
				console.warn("Failed to lock orientation", err);
			}
		}

		void lockOrientation();
	}, []);
}
