"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _reactNativeWebview = require("react-native-webview");
var FileSystem = _interopRequireWildcard(require("expo-file-system/legacy"));
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const PdfViewer = ({
  pdfUrl
}) => {
  const [localUri, setLocalUri] = (0, _react.useState)(null);
  const [error, setError] = (0, _react.useState)(null);
  // Android's WebView has NO native PDF renderer, so a file:// (or direct)
  // PDF loads blank. Route it through Google's gview embed using the
  // (public) remote URL — same approach as DocumentViewer for office docs.
  // iOS WKWebView renders PDFs natively, so we download + show the file.
  const isAndroid = _reactNative.Platform.OS === 'android';
  (0, _react.useEffect)(() => {
    if (isAndroid) {
      return;
    }
    let cancelled = false;
    const run = async () => {
      try {
        const target = FileSystem.cacheDirectory + `pdf-${Date.now()}.pdf`;
        const result = await FileSystem.downloadAsync(pdfUrl, target);
        if (cancelled) {
          return;
        }
        setLocalUri(result.uri);
      } catch (e) {
        if (cancelled) {
          return;
        }
        console.log(e?.message);
        setError(e?.message || 'Download failed');
      }
    };
    run();
    return () => {
      cancelled = true;
    };
  }, [pdfUrl, isAndroid]);
  if (isAndroid) {
    const gviewUrl = `https://docs.google.com/gview?embedded=true&url=${encodeURIComponent(pdfUrl)}`;
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
      style: styles.container,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNativeWebview.WebView, {
        source: {
          uri: gviewUrl
        },
        style: styles.webview,
        originWhitelist: ['*'],
        startInLoadingState: true,
        renderLoading: () => /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
          style: styles.center,
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.ActivityIndicator, {
            size: "large"
          })
        })
      })
    });
  }
  if (error) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
      style: styles.center,
      children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.Text, {
        children: ["Couldn't load PDF: ", error]
      })
    });
  }
  if (!localUri) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
      style: styles.center,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.ActivityIndicator, {
        size: "large"
      })
    });
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
    style: styles.container,
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNativeWebview.WebView, {
      source: {
        uri: localUri
      },
      style: styles.webview,
      originWhitelist: ['*'],
      allowFileAccess: true,
      allowFileAccessFromFileURLs: true,
      allowUniversalAccessFromFileURLs: true
    })
  });
};
var _default = exports.default = PdfViewer;
const styles = _reactNative.StyleSheet.create({
  container: {
    flex: 1,
    width: '100%',
    backgroundColor: '#fff'
  },
  webview: {
    flex: 1,
    backgroundColor: '#fff'
  },
  center: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center'
  }
});
//# sourceMappingURL=PdfView.js.map