"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = require("react");
const react_native_1 = require("react-native");
const image_editor_1 = require("@react-native-community/image-editor");
const ImageViewer_1 = require("./ImageViewer");
const percentCalculator_1 = require("./helpers/percentCalculator");
const window = react_native_1.Dimensions.get('window');
const w = window.width;
class ImageCropper extends react_1.PureComponent {
    constructor() {
        super(...arguments);
        this.state = {
            positionX: 0,
            positionY: 0,
            width: 0,
            height: 0,
            minScale: 1,
            adjustedHeight: 0,
            loading: true,
            fittedSize: {
                width: 0,
                height: 0,
            },
            prevImageUri: '',
        };
        this.init = () => {
            const { imageUri } = this.props;
            react_native_1.Image.getSize(imageUri, (width, height) => {
                const { setCropperParams, cropAreaWidth, cropAreaHeight } = this.props;
                const areaWidth = cropAreaWidth;
                const areaHeight = cropAreaHeight;
                const srcSize = { width, height };
                const fittedSize = { width: 0, height: 0 };
                let scale = 1;
                if (width > height) {
                    const ratio = w / height;
                    fittedSize.width = width * ratio;
                    fittedSize.height = w;
                }
                else if (width < height) {
                    const ratio = w / width;
                    fittedSize.width = w;
                    fittedSize.height = height * ratio;
                }
                else if (width === height) {
                    fittedSize.width = w;
                    fittedSize.height = w;
                }
                if (areaWidth < areaHeight || areaWidth === areaHeight) {
                    if (width < height) {
                        if (fittedSize.height < areaHeight) {
                            scale = Math.ceil((areaHeight / fittedSize.height) * 10) / 10;
                        }
                        else {
                            scale = Math.ceil((areaWidth / fittedSize.width) * 10) / 10;
                        }
                    }
                    else {
                        scale = Math.ceil((areaHeight / fittedSize.height) * 10) / 10;
                    }
                }
                scale = scale < 1 ? 1 : scale;
                this.setState(prevState => (Object.assign(Object.assign({}, prevState), { srcSize,
                    fittedSize, minScale: scale, loading: false })), () => {
                    setCropperParams(this.state);
                });
            }, () => { });
        };
        this.handleMove = ({ positionX, positionY, scale }) => {
            const { setCropperParams } = this.props;
            this.setState(prevState => (Object.assign(Object.assign({}, prevState), { positionX,
                positionY,
                scale })), () => setCropperParams(this.state));
        };
    }
    static getDerivedStateFromProps(props, state) {
        if (props.imageUri !== state.prevImageUri) {
            return {
                prevImageUri: props.imageUri,
                loading: true,
            };
        }
        return null;
    }
    componentDidMount() {
        this.init();
    }
    componentDidUpdate(prevProps) {
        const { imageUri } = this.props;
        if (imageUri && prevProps.imageUri !== imageUri) {
            this.init();
        }
    }
    render() {
        const { loading, fittedSize, minScale } = this.state;
        const { imageUri, cropAreaWidth, cropAreaHeight } = this.props;
        const areaWidth = cropAreaWidth;
        const areaHeight = cropAreaHeight;
        const imageWidth = fittedSize.width;
        const imageHeight = fittedSize.height;
        return !loading ? (<ImageViewer_1.default image={imageUri} areaWidth={areaWidth} areaHeight={areaHeight} imageWidth={imageWidth} imageHeight={imageHeight} minScale={minScale} onMove={this.handleMove} imageBackdropColor="green"/>) : null;
    }
}
ImageCropper.crop = (params) => {
    const { imageUri, cropSize, positionX, positionY, cropAreaSize, srcSize, fittedSize, scale, } = params;
    const offset = {
        x: 0,
        y: 0,
    };
    const cropAreaW = cropAreaSize ? cropAreaSize.width : w;
    const cropAreaH = cropAreaSize ? cropAreaSize.height : w;
    const wScale = cropAreaW / scale;
    const hScale = cropAreaH / scale;
    const percentCropperAreaW = percentCalculator_1.getPercentDiffNumberFromNumber(wScale, fittedSize.width);
    const percentRestW = 100 - percentCropperAreaW;
    const hiddenAreaW = percentCalculator_1.getPercentFromNumber(percentRestW, fittedSize.width);
    const percentCropperAreaH = percentCalculator_1.getPercentDiffNumberFromNumber(hScale, fittedSize.height);
    const percentRestH = 100 - percentCropperAreaH;
    const hiddenAreaH = percentCalculator_1.getPercentFromNumber(percentRestH, fittedSize.height);
    const x = hiddenAreaW / 2 - positionX;
    const y = hiddenAreaH / 2 - positionY;
    offset.x = x <= 0 ? 0 : x;
    offset.y = y <= 0 ? 0 : y;
    const srcPercentCropperAreaW = percentCalculator_1.getPercentDiffNumberFromNumber(offset.x, fittedSize.width);
    const srcPercentCropperAreaH = percentCalculator_1.getPercentDiffNumberFromNumber(offset.y, fittedSize.height);
    const offsetW = percentCalculator_1.getPercentFromNumber(srcPercentCropperAreaW, srcSize.width);
    const offsetH = percentCalculator_1.getPercentFromNumber(srcPercentCropperAreaH, srcSize.height);
    const sizeW = percentCalculator_1.getPercentFromNumber(percentCropperAreaW, srcSize.width);
    const sizeH = percentCalculator_1.getPercentFromNumber(percentCropperAreaH, srcSize.height);
    offset.x = Math.floor(offsetW);
    offset.y = Math.floor(offsetH);
    const cropData = {
        offset,
        size: {
            width: Math.round(sizeW),
            height: Math.round(sizeH),
        },
        displaySize: {
            width: Math.round(cropSize.width),
            height: Math.round(cropSize.height),
        },
    };
    return new Promise((resolve, reject) => image_editor_1.default.cropImage(imageUri, cropData)
        .then(resolve)
        .catch(reject));
};
ImageCropper.defaultProps = {
    cropAreaWidth: w,
    cropAreaHeight: w,
};
exports.default = ImageCropper;
