import { isEmbedded, getEmbeddedView } from '../embedding';
import { setFragmentCallbacks } from './frame-helper-for-android';
const superProto = org.nativescript.widgets.FragmentBase.prototype;
const FragmentClass = org.nativescript.widgets.FragmentBase.extend('com.tns.FragmentClass', {
    init() { },
    onHiddenChanged(hidden) {
        this._callbacks.onHiddenChanged(this, hidden, superProto.onHiddenChanged);
    },
    onCreateAnimator(transit, enter, nextAnim) {
        return this._callbacks.onCreateAnimator(this, transit, enter, nextAnim, superProto.onCreateAnimator);
    },
    onStop() {
        this._callbacks.onStop(this, superProto.onStop);
    },
    onPause() {
        this._callbacks.onPause(this, superProto.onPause);
    },
    onResume() {
        this._callbacks.onResume(this, superProto.onResume);
    },
    onCreate(savedInstanceState) {
        if (!this._callbacks) {
            setFragmentCallbacks(this);
        }
        this.setHasOptionsMenu(true);
        this._callbacks.onCreate(this, savedInstanceState, superProto.onCreate);
    },
    onCreateView(inflater, container, savedInstanceState) {
        return this._callbacks.onCreateView(this, inflater, container, savedInstanceState, superProto.onCreateView);
    },
    onSaveInstanceState(outState) {
        this._callbacks.onSaveInstanceState(this, outState, superProto.onSaveInstanceState);
    },
    onDestroyView() {
        this._callbacks.onDestroyView(this, superProto.onDestroyView);
    },
    onDestroy() {
        this._callbacks.onDestroy(this, superProto.onDestroy);
        this._callbacks = null;
    },
    toString() {
        const callbacks = this._callbacks;
        if (callbacks) {
            return callbacks.toStringOverride(this, superProto.toString);
        }
        else {
            superProto.toString();
        }
    },
});
export let fragmentClass;
export function ensureFragmentClass() {
    if (fragmentClass) {
        return;
    }
    // this require will apply the FragmentClass implementation
    // TODO: circular dependency issue with fragment require - check back on this when testing
    // require('./fragment');
    if (!fragmentClass) {
        throw new Error('Failed to initialize the extended androidx.fragment.app.Fragment class');
    }
}
export function setFragmentClass(clazz) {
    if (fragmentClass) {
        throw new Error('Fragment class already initialized');
    }
    if (isEmbedded()) {
        attachEmbeddableFragmentCallbacks();
    }
    fragmentClass = clazz;
}
function attachEmbeddableFragmentCallbacks() {
    const Callbacks = com.tns.embedding.EmbeddableFragmentCallbacks.extend({
        init() {
            // init must at least be defined
        },
        onCreateView() {
            return getEmbeddedView().nativeViewProtected;
        },
        onResume() {
            getEmbeddedView().callLoaded();
        },
        onPause() {
            getEmbeddedView().callUnloaded();
        },
    });
    com.tns.embedding.CallbacksStore.setFragmentCallbacks(new Callbacks());
}
setFragmentClass(FragmentClass);
//# sourceMappingURL=fragment.android.js.map