import { Transition } from '.';
import { isNumber } from '../../utils/types';
import { CORE_ANIMATION_DEFAULTS, getDurationWithDampingFromSpring } from '../../utils/animation-helpers';
import { GestureStateTypes } from '../gestures';
import { SharedTransition, SharedTransitionAnimationType } from './shared-transition';
import { SharedTransitionHelper } from './shared-transition-helper';
export class PageTransition extends Transition {
    iosNavigatedController(navigationController, operation, fromVC, toVC) {
        this.navigationController = navigationController;
        if (!this.transitionController) {
            this.presented = toVC;
            this.presenting = fromVC;
        }
        this.transitionController = PageTransitionController.initWithOwner(new WeakRef(this));
        // console.log('iosNavigatedController presenting:', this.presenting);
        this.operation = operation;
        return this.transitionController;
    }
    iosInteractionDismiss(animator) {
        // console.log('-- iosInteractionDismiss --');
        this.interactiveController = PercentInteractiveController.initWithOwner(new WeakRef(this));
        return this.interactiveController;
    }
    setupInteractiveGesture(startCallback, view) {
        // console.log(' -- setupInteractiveGesture --');
        this._interactiveStartCallback = startCallback;
        if (!this._interactiveDismissGesture) {
            // console.log('setup but tearing down first!');
            view.off('pan', this._interactiveDismissGesture);
            this._interactiveDismissGesture = this._interactiveDismissGestureHandler.bind(this);
        }
        view.on('pan', this._interactiveDismissGesture);
        this._interactiveGestureTeardown = () => {
            // console.log(`-- TEARDOWN setupInteractiveGesture --`);
            if (view) {
                view.off('pan', this._interactiveDismissGesture);
            }
            this._interactiveDismissGesture = null;
        };
        return this._interactiveGestureTeardown;
    }
    _interactiveDismissGestureHandler(args) {
        if (args?.ios?.view) {
            // console.log('this.id:', this.id);
            const state = SharedTransition.getState(this.id);
            if (!state) {
                // cleanup and exit, already shutdown
                this._teardownGesture();
                return;
            }
            const percent = state.interactive?.dismiss?.percentFormula ? state.interactive.dismiss.percentFormula(args) : args.deltaX / (args.ios.view.bounds.size.width / 2);
            if (SharedTransition.DEBUG) {
                console.log('Interactive dismissal percentage:', percent);
            }
            switch (args.state) {
                case GestureStateTypes.began:
                    SharedTransition.updateState(this.id, {
                        interactiveBegan: true,
                        interactiveCancelled: false,
                    });
                    if (this._interactiveStartCallback) {
                        this._interactiveStartCallback();
                    }
                    break;
                case GestureStateTypes.changed:
                    if (percent < 1) {
                        if (this.interactiveController) {
                            this.interactiveController.updateInteractiveTransition(percent);
                        }
                    }
                    break;
                case GestureStateTypes.cancelled:
                case GestureStateTypes.ended:
                    if (this.interactiveController) {
                        const finishThreshold = isNumber(state.interactive?.dismiss?.finishThreshold) ? state.interactive.dismiss.finishThreshold : 0.5;
                        if (percent > finishThreshold) {
                            this._teardownGesture();
                            this.interactiveController.finishInteractiveTransition();
                        }
                        else {
                            SharedTransition.updateState(this.id, {
                                interactiveCancelled: true,
                            });
                            this.interactiveController.cancelInteractiveTransition();
                        }
                    }
                    break;
            }
        }
    }
    _teardownGesture() {
        if (this._interactiveGestureTeardown) {
            this._interactiveGestureTeardown();
            this._interactiveGestureTeardown = null;
        }
    }
}
var PercentInteractiveController = (function (_super) {
    __extends(PercentInteractiveController, _super);
    function PercentInteractiveController() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    PercentInteractiveController.initWithOwner = function (owner) {
        var ctrl = PercentInteractiveController.new();
        ctrl.owner = owner;
        return ctrl;
    };
    PercentInteractiveController.prototype.startInteractiveTransition = function (transitionContext) {
        var _a;
        if (!this.interactiveState) {
            this.interactiveState = {
                transitionContext: transitionContext,
            };
            var owner = (_a = this.owner) === null || _a === void 0 ? void 0 : _a.deref();
            if (owner) {
                var state = SharedTransition.getState(owner.id);
                SharedTransitionHelper.interactiveStart(state, this.interactiveState, "page");
            }
        }
    };
    PercentInteractiveController.prototype.updateInteractiveTransition = function (percentComplete) {
        var _a;
        var owner = (_a = this.owner) === null || _a === void 0 ? void 0 : _a.deref();
        if (owner) {
            var state = SharedTransition.getState(owner.id);
            SharedTransitionHelper.interactiveUpdate(state, this.interactiveState, "page", percentComplete);
        }
    };
    PercentInteractiveController.prototype.cancelInteractiveTransition = function () {
        var _a;
        var owner = (_a = this.owner) === null || _a === void 0 ? void 0 : _a.deref();
        if (owner) {
            var state = SharedTransition.getState(owner.id);
            SharedTransitionHelper.interactiveCancel(state, this.interactiveState, "page");
        }
    };
    PercentInteractiveController.prototype.finishInteractiveTransition = function () {
        var _a;
        var owner = (_a = this.owner) === null || _a === void 0 ? void 0 : _a.deref();
        if (owner) {
            var state = SharedTransition.getState(owner.id);
            SharedTransitionHelper.interactiveFinish(state, this.interactiveState, "page");
        }
    };
    PercentInteractiveController.ObjCProtocols = [UIViewControllerInteractiveTransitioning];
    return PercentInteractiveController;
}(UIPercentDrivenInteractiveTransition));
var PageTransitionController = (function (_super) {
    __extends(PageTransitionController, _super);
    function PageTransitionController() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    PageTransitionController.initWithOwner = function (owner) {
        var ctrl = PageTransitionController.new();
        ctrl.owner = owner;
        return ctrl;
    };
    PageTransitionController.prototype.transitionDuration = function (transitionContext) {
        var _a, _b, _c, _d, _e, _f;
        var owner = this.owner.deref();
        if (owner) {
            var state = SharedTransition.getState(owner.id);
            switch (state === null || state === void 0 ? void 0 : state.activeType) {
                case SharedTransitionAnimationType.present: if (isNumber((_a = state === null || state === void 0 ? void 0 : state.pageEnd) === null || _a === void 0 ? void 0 : _a.duration)) {
                    return ((_b = state.pageEnd) === null || _b === void 0 ? void 0 : _b.duration) / 1000;
                }
                else {
                    return getDurationWithDampingFromSpring((_c = state.pageEnd) === null || _c === void 0 ? void 0 : _c.spring).duration;
                }
                case SharedTransitionAnimationType.dismiss: if (isNumber((_d = state === null || state === void 0 ? void 0 : state.pageReturn) === null || _d === void 0 ? void 0 : _d.duration)) {
                    return ((_e = state.pageReturn) === null || _e === void 0 ? void 0 : _e.duration) / 1000;
                }
                else {
                    return getDurationWithDampingFromSpring((_f = state.pageReturn) === null || _f === void 0 ? void 0 : _f.spring).duration;
                }
            }
        }
        return CORE_ANIMATION_DEFAULTS.duration;
    };
    PageTransitionController.prototype.animateTransition = function (transitionContext) {
        var owner = this.owner.deref();
        if (owner) {
            var state = SharedTransition.getState(owner.id);
            if (!state) {
                return;
            }
            SharedTransitionHelper.animate(state, transitionContext, "page");
        }
    };
    PageTransitionController.ObjCProtocols = [UIViewControllerAnimatedTransitioning];
    return PageTransitionController;
}(NSObject));
//# sourceMappingURL=page-transition.ios.js.map