import { Color } from '../../../color';
import { RootLayoutBase, defaultShadeCoverOptions } from './root-layout-common';
import { parseLinearGradient } from '../../../css/parser';
import { LinearGradient } from '../../styling/linear-gradient';
export * from './root-layout-common';
export class RootLayout extends RootLayoutBase {
    insertChild(view, atIndex) {
        super.insertChild(view, atIndex);
        if (!view.hasGestureObservers()) {
            // block tap events from going through to layers behind the view
            if (view.nativeViewProtected) {
                view.nativeViewProtected.setOnTouchListener(new android.view.View.OnTouchListener({
                    onTouch: function (view, event) {
                        return true;
                    },
                }));
            }
        }
    }
    removeChild(view) {
        if (view.hasGestureObservers() && view.nativeViewProtected) {
            view.nativeViewProtected.setOnTouchListener(null);
        }
        super.removeChild(view);
    }
    _bringToFront(view) {
        view.nativeViewProtected.bringToFront();
    }
    _initShadeCover(view, shadeOptions) {
        const initialState = {
            ...defaultShadeCoverOptions.animation.enterFrom,
            ...shadeOptions?.animation?.enterFrom,
        };
        this._playAnimation(this._getAnimationSet(view, initialState));
    }
    _updateShadeCover(view, shadeOptions) {
        const options = {
            ...defaultShadeCoverOptions,
            ...shadeOptions,
        };
        const duration = options.animation?.enterFrom?.duration || defaultShadeCoverOptions.animation.enterFrom.duration;
        return this._playAnimation(this._getAnimationSet(view, {
            translateX: 0,
            translateY: 0,
            scaleX: 1,
            scaleY: 1,
            rotate: 0,
            opacity: options.opacity,
        }, options.color), duration);
    }
    _closeShadeCover(view, shadeOptions) {
        const exitState = {
            ...defaultShadeCoverOptions.animation.exitTo,
            ...shadeOptions?.animation?.exitTo,
        };
        return this._playAnimation(this._getAnimationSet(view, exitState), exitState?.duration);
    }
    _getAnimationSet(view, shadeCoverAnimation, backgroundColor = defaultShadeCoverOptions.color) {
        const isBackgroundGradient = backgroundColor && backgroundColor.startsWith('linear-gradient');
        const animationSet = Array.create(android.animation.Animator, isBackgroundGradient ? 6 : 7);
        animationSet[0] = android.animation.ObjectAnimator.ofFloat(view.nativeViewProtected, 'translationX', [shadeCoverAnimation.translateX]);
        animationSet[1] = android.animation.ObjectAnimator.ofFloat(view.nativeViewProtected, 'translationY', [shadeCoverAnimation.translateY]);
        animationSet[2] = android.animation.ObjectAnimator.ofFloat(view.nativeViewProtected, 'scaleX', [shadeCoverAnimation.scaleX]);
        animationSet[3] = android.animation.ObjectAnimator.ofFloat(view.nativeViewProtected, 'scaleY', [shadeCoverAnimation.scaleY]);
        animationSet[4] = android.animation.ObjectAnimator.ofFloat(view.nativeViewProtected, 'rotation', [shadeCoverAnimation.rotate]);
        animationSet[5] = android.animation.ObjectAnimator.ofFloat(view.nativeViewProtected, 'alpha', [shadeCoverAnimation.opacity]);
        if (isBackgroundGradient) {
            if (view.backgroundColor) {
                view.backgroundColor = undefined;
            }
            const parsedGradient = parseLinearGradient(backgroundColor);
            view.backgroundImage = LinearGradient.parse(parsedGradient.value);
        }
        else {
            if (view.backgroundImage) {
                view.backgroundImage = undefined;
            }
            animationSet[6] = this._getBackgroundColorAnimator(view, backgroundColor);
        }
        return animationSet;
    }
    _getBackgroundColorAnimator(view, backgroundColor) {
        const nativeArray = Array.create(java.lang.Object, 2);
        nativeArray[0] = java.lang.Integer.valueOf(view.backgroundColor ? view.backgroundColor.argb : -1);
        nativeArray[1] = java.lang.Integer.valueOf(backgroundColor ? new Color(backgroundColor).argb : -1);
        const backgroundColorAnimator = android.animation.ValueAnimator.ofObject(new android.animation.ArgbEvaluator(), nativeArray);
        backgroundColorAnimator.addUpdateListener(new android.animation.ValueAnimator.AnimatorUpdateListener({
            onAnimationUpdate(animator) {
                const argb = animator.getAnimatedValue().intValue();
                view.backgroundColor = new Color(argb);
            },
        }));
        return backgroundColorAnimator;
    }
    _playAnimation(animationSet, duration = 0) {
        return new Promise((resolve) => {
            const animatorSet = new android.animation.AnimatorSet();
            animatorSet.playTogether(animationSet);
            animatorSet.setDuration(duration);
            animatorSet.addListener(new android.animation.Animator.AnimatorListener({
                onAnimationStart: function (animator) { },
                onAnimationEnd: function (animator) {
                    resolve();
                },
                onAnimationRepeat: function (animator) { },
                onAnimationCancel: function (animator) { },
            }));
            animatorSet.start();
        });
    }
}
//# sourceMappingURL=index.android.js.map