// index.ts
import { NativeModules, requireNativeComponent } from "react-native";
import { BaseDocumentEngine } from "@papyrus-sdk/core";
var MODULE_NAME = "PapyrusNativeEngine";
var PapyrusPageView = requireNativeComponent("PapyrusPageView");
var NativeDocumentEngine = class extends BaseDocumentEngine {
  constructor() {
    super();
    this.nativeModule = null;
    this.engineId = "default";
    this.pageCount = 0;
    this.currentPage = 1;
    this.zoom = 1;
    this.rotation = 0;
    this.nativeModule = NativeModules[MODULE_NAME] ?? null;
    this.engineId = this.nativeModule?.createEngine ? this.nativeModule.createEngine() : "default";
  }
  async load(source) {
    const native = this.assertNativeModule();
    const normalized = await this.normalizeSource(source);
    const result = native.load ? await native.load(this.engineId, normalized) : void 0;
    if (result && typeof result.pageCount === "number") {
      this.pageCount = result.pageCount;
    } else if (native.getPageCount) {
      this.pageCount = native.getPageCount(this.engineId);
    } else {
      this.pageCount = 0;
    }
    this.currentPage = 1;
  }
  getPageCount() {
    return this.pageCount;
  }
  getCurrentPage() {
    return this.currentPage;
  }
  goToPage(page) {
    if (page >= 1 && page <= this.pageCount) {
      this.currentPage = page;
    }
  }
  setZoom(zoom) {
    this.zoom = Math.max(0.1, Math.min(5, zoom));
  }
  getZoom() {
    return this.zoom;
  }
  rotate(direction) {
    if (direction === "clockwise") {
      this.rotation = (this.rotation + 90) % 360;
    } else {
      this.rotation = (this.rotation - 90) % 360;
      if (this.rotation < 0) this.rotation += 360;
    }
  }
  getRotation() {
    return this.rotation;
  }
  async renderPage(pageIndex, target, scale) {
    const native = this.assertNativeModule();
    if (!native.renderPage) return;
    const viewTag = this.toNativeViewTag(target);
    if (viewTag === null) return;
    native.renderPage(this.engineId, pageIndex, viewTag, scale, this.zoom, this.rotation);
  }
  async renderTextLayer(pageIndex, target, scale) {
    const native = this.assertNativeModule();
    if (!native.renderTextLayer) return;
    const viewTag = this.toNativeViewTag(target);
    if (viewTag === null) return;
    native.renderTextLayer(this.engineId, pageIndex, viewTag, scale, this.zoom, this.rotation);
  }
  async getTextContent(pageIndex) {
    const native = this.assertNativeModule();
    if (!native.getTextContent) return [];
    return native.getTextContent(this.engineId, pageIndex);
  }
  async getPageDimensions(pageIndex) {
    const native = this.assertNativeModule();
    if (!native.getPageDimensions) return { width: 0, height: 0 };
    return native.getPageDimensions(this.engineId, pageIndex);
  }
  async selectText(pageIndex, rect) {
    const native = this.assertNativeModule();
    if (!native.selectText) return null;
    return native.selectText(this.engineId, pageIndex, rect.x, rect.y, rect.width, rect.height);
  }
  async getOutline() {
    const native = this.assertNativeModule();
    if (!native.getOutline) return [];
    return native.getOutline(this.engineId);
  }
  async searchText(query) {
    const native = this.assertNativeModule();
    if (!native.searchText) return [];
    return native.searchText(this.engineId, query);
  }
  async getPageIndex(dest) {
    const native = this.assertNativeModule();
    if (!native.getPageIndex) return null;
    return native.getPageIndex(this.engineId, dest);
  }
  destroy() {
    this.nativeModule?.destroyEngine?.(this.engineId);
  }
  assertNativeModule() {
    if (!this.nativeModule) {
      throw new Error(`[Papyrus] Native module "${MODULE_NAME}" not found. Did you run pod install / gradle sync?`);
    }
    return this.nativeModule;
  }
  async normalizeSource(source) {
    if (typeof source === "string") return { uri: source };
    if (this.isUriSource(source)) return { uri: source.uri };
    if (this.isDataSource(source)) return { data: source.data };
    if (this.isFileLike(source)) return { data: await source.arrayBuffer() };
    if (source instanceof ArrayBuffer || source instanceof Uint8Array) return { data: source };
    return { data: source };
  }
  isUriSource(source) {
    return typeof source === "object" && source !== null && "uri" in source;
  }
  isDataSource(source) {
    return typeof source === "object" && source !== null && "data" in source;
  }
  isFileLike(source) {
    return typeof source === "object" && source !== null && typeof source.arrayBuffer === "function";
  }
  toNativeViewTag(target) {
    if (typeof target === "number") return target;
    if (target?.nativeTag && typeof target.nativeTag === "number") return target.nativeTag;
    return null;
  }
};
export {
  NativeDocumentEngine,
  PapyrusPageView
};
//# sourceMappingURL=index.mjs.map