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 React, { Component } from "react";
import { View, TouchableOpacity, Modal, Dimensions, Alert } from "react-native";
import { Text } from "./Text";
import { Icon } from "@rneui/themed";
import { WebView } from "react-native-webview";
import { File, Paths } from "expo-file-system";
import * as Sharing from "expo-sharing";
import { FontAwesome } from "./Icon";
import { Spinner } from "./Spinner";
import { StatusBarHeight, getFileName } from "../helper";
const SCREEN_HEIGHT = Dimensions.get("window").height;
class PDFPopup extends Component {
  constructor(...args) {
    super(...args);
    _defineProperty(this, "state", {
      loaded: 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"
        }]);
      }
    });
  }
  onLoadStarted() {
    this.setState({
      loaded: false
    });
  }
  onLoadEnded() {
    this.setState({
      loaded: true
    });
  }
  onChangeIndex(index) {
    this.props.onChangeIndex(index);
  }
  getUrl() {
    const linkPrefix = "http://drive.google.com/viewerng/viewer?embedded=true&url=";
    return `${linkPrefix}${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.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: ["*"],
      mixedContentMode: "always",
      renderError: e => {
        console.log("errored");
        console.log(e);
      },
      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"
  }
};
export { PDFPopup };
//# sourceMappingURL=PDFPopup.js.map