function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import { Icon } from "@rneui/themed";
import { File, Paths } from "expo-file-system";
import * as Sharing from "expo-sharing";
import { Component } from "react";
import { Alert, Dimensions, Modal, TouchableOpacity, View } from "react-native";
import { WebView } from "react-native-webview";
import { TEXT_DARK, TEXT_LIGHT } from "../colours";
import { getFileName, StatusBarHeight } from "../helper";
import { FontAwesome } from "./Icon";
import { Spinner } from "./Spinner";
import { Text } from "./Text";
const SCREEN_HEIGHT = Dimensions.get("window").height;
class PDFPopup extends Component {
  constructor(...args) {
    super(...args);
    _defineProperty(this, "state", {
      loaded: false,
      error: false
    });
    _defineProperty(this, "download", async () => {
      try {
        const file = await File.downloadFileAsync(this.props.source, new File(Paths.document, getFileName(this.props.source)), {
          idempotent: true
        });
        await Sharing.shareAsync(file.uri, {
          UTI: "public.item"
        });
      } catch (error) {
        console.log("PDFPopup download error:", error);
        Alert.alert("Download Failed", "Unable to download PDF. Please try again.", [{
          text: "OK"
        }]);
      }
    });
  }
  componentDidUpdate(prevProps) {
    const sourceChanged = prevProps.source !== this.props.source;
    const becameVisible = !prevProps.visible && this.props.visible;
    if (sourceChanged || becameVisible) {
      // Popup is reused across attachments — clear stale state so the
      // spinner reappears and any prior error from a different PDF resets.
      clearTimeout(this._loadTimeout);
      this._loadTimeout = null;
      this.setState({
        loaded: false,
        error: false
      });
    }
  }
  componentWillUnmount() {
    clearTimeout(this._loadTimeout);
    this._loadTimeout = null;
  }
  onLoadStarted() {
    this.setState({
      loaded: false,
      error: false
    });
    clearTimeout(this._loadTimeout);
    this._loadTimeout = setTimeout(() => {
      this.setState({
        error: true
      });
    }, 15000);
  }
  onLoadEnded() {
    clearTimeout(this._loadTimeout);
    this._loadTimeout = null;
    this.setState({
      loaded: true
    });
  }
  onChangeIndex(index) {
    this.props.onChangeIndex(index);
  }
  getUrl() {
    if (!this.props.source) {
      return "";
    }
    const linkPrefix = "https://drive.google.com/viewerng/viewer?embedded=true&url=";
    return `${linkPrefix}${encodeURIComponent(this.props.source)}`;
  }
  closePopup() {
    this.props.onClose();
  }
  renderFooter() {
    if (this.props.pdfCount && this.props.pdfCount === 1) {
      return null;
    }
    return /*#__PURE__*/React.createElement(View, {
      style: styles.footerContent
    }, /*#__PURE__*/React.createElement(TouchableOpacity, {
      onPress: this.onChangeIndex.bind(this, -1),
      activeOpacity: 0.6
    }, /*#__PURE__*/React.createElement(Icon, {
      type: "font-awesome",
      name: "angle-left",
      iconStyle: [styles.chevron]
    })), /*#__PURE__*/React.createElement(Text, {
      style: styles.footerText
    }, this.props.selectedIndex + 1, " / ", this.props.pdfCount), /*#__PURE__*/React.createElement(TouchableOpacity, {
      onPress: this.onChangeIndex.bind(this, 1),
      activeOpacity: 0.6
    }, /*#__PURE__*/React.createElement(Icon, {
      type: "font-awesome",
      name: "angle-right",
      iconStyle: [styles.chevron]
    })));
  }
  render() {
    if (!this.props.source) {
      return null;
    }
    return /*#__PURE__*/React.createElement(Modal, {
      visible: this.props.visible,
      transparent: true,
      animationType: "slide",
      onRequestClose: this.closePopup.bind(this)
    }, /*#__PURE__*/React.createElement(View, {
      style: {
        flex: 1,
        backgroundColor: "#000",
        paddingTop: StatusBarHeight(0)
      }
    }, /*#__PURE__*/React.createElement(View, {
      style: styles.headerContent
    }, /*#__PURE__*/React.createElement(TouchableOpacity, {
      onPress: this.closePopup.bind(this),
      activeOpacity: 0.6
    }, /*#__PURE__*/React.createElement(FontAwesome, {
      name: "x",
      style: [styles.menuIcon]
    })), /*#__PURE__*/React.createElement(View, {
      style: styles.titleContainer
    }, /*#__PURE__*/React.createElement(Text, {
      style: styles.title
    }, this.props.title)), /*#__PURE__*/React.createElement(TouchableOpacity, {
      onPress: this.download,
      activeOpacity: 0.6
    }, /*#__PURE__*/React.createElement(Icon, {
      type: "font-awesome",
      name: "cloud-download",
      iconStyle: [styles.menuIcon]
    }))), /*#__PURE__*/React.createElement(View, {
      style: {
        flex: 1,
        backgroundColor: "#fff"
      }
    }, this.state.error ? /*#__PURE__*/React.createElement(View, {
      style: styles.errorContainer
    }, /*#__PURE__*/React.createElement(Text, {
      style: styles.errorTitle
    }, "Unable to load this PDF."), /*#__PURE__*/React.createElement(Text, {
      style: styles.errorSubtitle
    }, "Try downloading it instead."), /*#__PURE__*/React.createElement(TouchableOpacity, {
      onPress: this.download,
      activeOpacity: 0.6,
      style: styles.errorDownloadButton
    }, /*#__PURE__*/React.createElement(Icon, {
      type: "font-awesome",
      name: "cloud-download",
      iconStyle: styles.errorDownloadIcon
    }), /*#__PURE__*/React.createElement(Text, {
      style: styles.errorDownloadText
    }, "Download PDF"))) : /*#__PURE__*/React.createElement(React.Fragment, null, this.state.loaded ? null : /*#__PURE__*/React.createElement(Spinner, {
      style: {
        height: SCREEN_HEIGHT - StatusBarHeight(145),
        flex: 0
      }
    }), /*#__PURE__*/React.createElement(WebView, {
      source: {
        uri: this.getUrl()
      },
      javaScriptEnabled: true,
      domStorageEnabled: true,
      originWhitelist: ["*"],
      renderError: () => {
        // Surface error UI in the parent View rather than the
        // default WebView error chrome. Defer setState to avoid
        // triggering a state update during another render.
        if (!this.state.error) {
          setTimeout(() => this.setState({
            error: true
          }), 0);
        }
        return null;
      },
      style: [{
        flex: 1,
        backgroundColor: "#fff"
      }],
      onLoadStart: this.onLoadStarted.bind(this),
      onLoadEnd: this.onLoadEnded.bind(this)
    }))), this.renderFooter()));
  }
}
const styles = {
  container: {
    flex: 1,
    position: "relative",
    backgroundColor: "#000"
  },
  innerContainer: {
    flex: 1,
    paddingLeft: 15,
    paddingRight: 15
  },
  menuIcon: {
    fontSize: 25,
    padding: 15,
    width: 55,
    textAlign: "center",
    color: "#fff",
    zIndex: 3
  },
  headerContent: {
    flexDirection: "row",
    alignItems: "center",
    height: 70
  },
  titleContainer: {
    flex: 1
  },
  title: {
    color: "#fff",
    fontFamily: "sf-semibold",
    fontSize: 16,
    textAlign: "center"
  },
  footerContent: {
    flexDirection: "row",
    alignItems: "center",
    justifyContent: "center",
    height: 70
    //alignSelf: 'flex-end'
  },
  chevron: {
    fontSize: 25,
    padding: 15,
    width: 55,
    textAlign: "center",
    color: "#fff",
    zIndex: 3
  },
  footerText: {
    fontFamily: "sf-regular",
    fontSize: 14,
    color: "#fff",
    width: 150,
    textAlign: "center"
  },
  errorContainer: {
    flex: 1,
    alignItems: "center",
    justifyContent: "center",
    paddingHorizontal: 32,
    paddingVertical: 24,
    backgroundColor: "#fff"
  },
  errorTitle: {
    fontFamily: "sf-semibold",
    fontSize: 18,
    color: TEXT_DARK,
    textAlign: "center"
  },
  errorSubtitle: {
    fontFamily: "sf-regular",
    fontSize: 14,
    color: TEXT_LIGHT,
    textAlign: "center",
    marginTop: 8
  },
  errorDownloadButton: {
    flexDirection: "row",
    alignItems: "center",
    justifyContent: "center",
    marginTop: 24,
    paddingHorizontal: 20,
    paddingVertical: 12
  },
  errorDownloadIcon: {
    fontSize: 20,
    color: TEXT_DARK,
    marginRight: 8
  },
  errorDownloadText: {
    fontFamily: "sf-semibold",
    fontSize: 16,
    color: TEXT_DARK
  }
};
export { PDFPopup };
//# sourceMappingURL=PDFPopup.js.map