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 _ from "lodash";
import { TouchableOpacity, StyleSheet, View, Alert } from "react-native";
import { Text } from "./Text";
import { Icon } from "@rneui/themed";
import { File, Paths } from "expo-file-system";
import * as Sharing from "expo-sharing";
import * as MediaLibrary from "expo-media-library";
import { StatusBarHeight, isVideo, getMimeType } from "../helper";
import { MiddlePopup } from "./MiddlePopup";
import Config from "../config";
class SharingTools extends Component {
  constructor(props) {
    super(props);
    _defineProperty(this, "downloadMedia", async () => {
      var _parts$;
      const remoteUrl = this.props.uri;
      if (!remoteUrl) {
        throw new Error("No media URL provided");
      }
      const parts = remoteUrl.split("/").splice(-2);
      const mediaId = parts[0];
      const extension = ((_parts$ = parts[1]) === null || _parts$ === void 0 ? void 0 : _parts$.split(".").splice(-1)[0]) || "jpg";
      const file = await File.downloadFileAsync(remoteUrl, new File(Paths.cache, `${mediaId}.${extension}`), {
        idempotent: true
      });
      return {
        localUrl: file.uri,
        extension
      };
    });
    _defineProperty(this, "onSave", async () => {
      try {
        // Request permissions first
        const {
          status
        } = await MediaLibrary.requestPermissionsAsync();
        if (status !== "granted") {
          Alert.alert("Permission Required", "Please allow access to your photo library to save photos.", [{
            text: "OK"
          }]);
          return;
        }
        this.setState({
          message: "Saving..."
        });
        const {
          localUrl
        } = await this.downloadMedia();
        await MediaLibrary.saveToLibraryAsync(localUrl);
        this.setState({
          message: "Saved"
        }, () => {
          setTimeout(() => {
            this.setState({
              message: ""
            });
          }, 2000);
        });
      } catch (error) {
        console.log("SharingTools onSave error:", error);
        this.setState({
          message: ""
        });
        Alert.alert("Save Failed", "Unable to save photo. Please try again.", [{
          text: "OK"
        }]);
      }
    });
    _defineProperty(this, "onShare", async () => {
      if (!(await Sharing.isAvailableAsync())) {
        Alert.alert("Sharing Unavailable", "Sharing is not available on this device.", [{
          text: "OK"
        }]);
        return;
      }
      try {
        this.setState({
          message: "Preparing..."
        });
        const {
          localUrl,
          extension
        } = await this.downloadMedia();
        const video = isVideo(localUrl);
        const mimeType = getMimeType(localUrl);
        const fileDetails = {
          extension: `.${extension}`,
          shareOptions: {
            mimeType,
            dialogTitle: video ? "Share video" : "Share photo",
            UTI: mimeType
          }
        };
        await Sharing.shareAsync(localUrl, fileDetails.shareOptions);
        this.setState({
          message: ""
        });
      } catch (error) {
        console.log("SharingTools onShare error:", error);
        this.setState({
          message: ""
        });
        Alert.alert("Share Failed", "Unable to share photo. Please try again.", [{
          text: "OK"
        }]);
      }
    });
    this.state = {
      message: ""
    };
  }
  renderTools() {
    return /*#__PURE__*/React.createElement(View, {
      style: styles.toolsContainer
    }, Config.env.allowMediaDownload && /*#__PURE__*/React.createElement(TouchableOpacity, {
      onPress: this.onSave,
      activeOpacity: 0.6
    }, /*#__PURE__*/React.createElement(Icon, {
      name: "download",
      type: "font-awesome",
      iconStyle: styles.toolIcon,
      style: styles.toolIconContainer
    })), Config.env.allowMediaSharing && /*#__PURE__*/React.createElement(TouchableOpacity, {
      onPress: this.onShare,
      activeOpacity: 0.6
    }, /*#__PURE__*/React.createElement(Icon, {
      name: "share-square-o",
      type: "font-awesome",
      iconStyle: styles.toolIcon,
      style: styles.toolIconContainer
    })));
  }
  renderMessage() {
    return /*#__PURE__*/React.createElement(MiddlePopup, {
      visible: !_.isEmpty(this.state.message)
    }, /*#__PURE__*/React.createElement(View, {
      style: styles.messageContainer
    }, /*#__PURE__*/React.createElement(Text, {
      style: styles.messageText
    }, this.state.message)));
  }
  render() {
    return /*#__PURE__*/React.createElement(View, {
      style: styles.container
    }, this.renderTools(), this.renderMessage());
  }
}
const styles = StyleSheet.create({
  container: {
    position: "absolute"
  },
  toolsContainer: {
    position: "absolute",
    flexDirection: "row",
    justifyContent: "space-between",
    left: 0,
    zIndex: 3,
    top: StatusBarHeight(0)
  },
  toolIconContainer: {
    width: 55,
    height: 55
  },
  toolIcon: {
    fontSize: 25,
    padding: 15,
    width: 55,
    textAlign: "center",
    color: "#fff",
    zIndex: 3
  },
  messageContainer: {
    backgroundColor: "#000",
    borderRadius: 10,
    paddingVertical: 10,
    paddingHorizontal: 30
  },
  messageText: {
    fontFamily: "sf-bold",
    fontSize: 24,
    color: "#fff"
  }
});
export { SharingTools };
//# sourceMappingURL=SharingTools.js.map