import { getNativeApp, updateA11yPropertiesCallback } from './helpers-common';
import { AccessibilityRole, AccessibilityState } from '../accessibility/accessibility-common';
import { Trace } from '../trace';
let _startActivity;
let _foregroundActivity;
export function androidGetCurrentActivity() {
    return _foregroundActivity || _startActivity;
}
export function androidGetForegroundActivity() {
    return _foregroundActivity;
}
export function androidSetForegroundActivity(activity) {
    _foregroundActivity = activity;
}
export function androidGetStartActivity() {
    return _startActivity;
}
export function androidSetStartActivity(activity) {
    _startActivity = activity;
}
function getApplicationContext() {
    return getNativeApp().getApplicationContext();
}
export function updateContentDescription(view /* View */, forceUpdate) {
    if (!view.nativeViewProtected) {
        return;
    }
    return applyContentDescription(view, forceUpdate);
}
export function applyContentDescription(view /* View */, forceUpdate) {
    let androidView = view.nativeViewProtected;
    if (!androidView || (androidView instanceof android.widget.TextView && !view._androidContentDescriptionUpdated)) {
        return null;
    }
    if (androidView instanceof androidx.appcompat.widget.Toolbar) {
        const numChildren = androidView.getChildCount();
        for (let i = 0; i < numChildren; i += 1) {
            const childAndroidView = androidView.getChildAt(i);
            if (childAndroidView instanceof androidx.appcompat.widget.AppCompatTextView) {
                androidView = childAndroidView;
                break;
            }
        }
    }
    const cls = `applyContentDescription(${view})`;
    const titleValue = view['title'];
    const textValue = view['text'];
    if (!forceUpdate && view._androidContentDescriptionUpdated === false && textValue === view['_lastText'] && titleValue === view['_lastTitle']) {
        // prevent updating this too much
        return androidView.getContentDescription();
    }
    const contentDescriptionBuilder = new Array();
    // Workaround: TalkBack won't read the checked state for fake Switch.
    if (view.accessibilityRole === AccessibilityRole.Switch) {
        const androidSwitch = new android.widget.Switch(getApplicationContext());
        if (view.accessibilityState === AccessibilityState.Checked) {
            contentDescriptionBuilder.push(androidSwitch.getTextOn());
        }
        else {
            contentDescriptionBuilder.push(androidSwitch.getTextOff());
        }
    }
    if (view.accessibilityLabel) {
        if (Trace.isEnabled()) {
            Trace.write(`${cls} - have accessibilityLabel`, Trace.categories.Accessibility);
        }
        contentDescriptionBuilder.push(`${view.accessibilityLabel}`);
    }
    if (view.accessibilityValue) {
        if (Trace.isEnabled()) {
            Trace.write(`${cls} - have accessibilityValue`, Trace.categories.Accessibility);
        }
        contentDescriptionBuilder.push(`${view.accessibilityValue}`);
    }
    else if (textValue) {
        if (textValue !== view.accessibilityLabel) {
            if (Trace.isEnabled()) {
                Trace.write(`${cls} - don't have accessibilityValue - use 'text' value`, Trace.categories.Accessibility);
            }
            contentDescriptionBuilder.push(`${textValue}`);
        }
    }
    else if (titleValue) {
        if (titleValue !== view.accessibilityLabel) {
            if (Trace.isEnabled()) {
                Trace.write(`${cls} - don't have accessibilityValue - use 'title' value`, Trace.categories.Accessibility);
            }
            contentDescriptionBuilder.push(`${titleValue}`);
        }
    }
    if (view.accessibilityHint) {
        if (Trace.isEnabled()) {
            Trace.write(`${cls} - have accessibilityHint`, Trace.categories.Accessibility);
        }
        contentDescriptionBuilder.push(`${view.accessibilityHint}`);
    }
    const contentDescription = contentDescriptionBuilder.join('. ').trim().replace(/^\.$/, '');
    if (contentDescription) {
        if (Trace.isEnabled()) {
            Trace.write(`${cls} - set to "${contentDescription}"`, Trace.categories.Accessibility);
        }
        androidView.setContentDescription(contentDescription);
    }
    else {
        if (Trace.isEnabled()) {
            Trace.write(`${cls} - remove value`, Trace.categories.Accessibility);
        }
        androidView.setContentDescription(null);
    }
    view['_lastTitle'] = titleValue;
    view['_lastText'] = textValue;
    view._androidContentDescriptionUpdated = false;
    return contentDescription;
}
export function setupAccessibleView(view /* any */) {
    updateA11yPropertiesCallback(view);
}
// stubs
//# sourceMappingURL=helpers.android.js.map