import { Color } from '../../../color';
import { RootLayoutBase, defaultShadeCoverOptions } from './root-layout-common';
import { parseLinearGradient } from '../../../css/parser';
import { LinearGradient } from '../../styling/linear-gradient';
import { layout } from '../../../utils';
export * from './root-layout-common';
export class RootLayout extends RootLayoutBase {
    insertChild(view, atIndex) {
        if (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;
                        },
                    }));
                }
            }
            return true;
        }
        return false;
    }
    removeChild(view) {
        if (view.hasGestureObservers() && view.nativeViewProtected) {
            view.nativeViewProtected.setOnTouchListener(null);
        }
        super.removeChild(view);
    }
    _bringToFront(view) {
        view.nativeViewProtected.bringToFront();
    }
    _initShadeCover(view, shadeOptions) {
        const options = {
            ...defaultShadeCoverOptions.animation.enterFrom,
            ...shadeOptions?.animation?.enterFrom,
        };
        const nativeView = view?.nativeViewProtected;
        if (nativeView) {
            nativeView.setAlpha(options.opacity);
            nativeView.setScaleX(float(options.scaleX));
            nativeView.setScaleY(float(options.scaleY));
            nativeView.setTranslationX(layout.toDevicePixels(options.translateX));
            nativeView.setTranslationY(layout.toDevicePixels(options.translateY));
            nativeView.setRotation(float(options.rotate));
        }
    }
    _updateShadeCover(view, shadeOptions) {
        const options = {
            ...defaultShadeCoverOptions,
            ...shadeOptions,
        };
        const duration = options.animation?.enterFrom?.duration || defaultShadeCoverOptions.animation.enterFrom.duration;
        const isBackgroundGradient = options.color && options.color.startsWith('linear-gradient');
        if (isBackgroundGradient) {
            if (view.backgroundColor) {
                view.backgroundColor = undefined;
            }
            const parsedGradient = parseLinearGradient(options.color);
            view.backgroundImage = LinearGradient.parse(parsedGradient.value);
        }
        else {
            if (view.backgroundImage) {
                view.backgroundImage = undefined;
            }
        }
        return this._playAnimation(this._getAnimationSet(view, {
            translateX: 0,
            translateY: 0,
            scaleX: 1,
            scaleY: 1,
            rotate: 0,
            opacity: options.opacity,
        }, isBackgroundGradient ? null : 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) {
        const animationSet = Array.create(android.animation.Animator, backgroundColor ? 7 : 6);
        animationSet[0] = android.animation.ObjectAnimator.ofFloat(view.nativeViewProtected, 'translationX', [layout.toDevicePixels(shadeCoverAnimation.translateX)]);
        animationSet[1] = android.animation.ObjectAnimator.ofFloat(view.nativeViewProtected, 'translationY', [layout.toDevicePixels(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 (backgroundColor) {
            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