import { ObservableArray, Builder, Utils, StackLayout, Color, Font, EditableTextBase, colorProperty, fontSizeProperty, fontInternalProperty, heightProperty, backgroundImageProperty, borderLeftWidthProperty, borderRightWidthProperty, borderTopWidthProperty, borderBottomWidthProperty, paddingBottomProperty, paddingTopProperty, paddingLeftProperty, paddingRightProperty, widthProperty, } from "@nativescript/core";
function getSdkApiLevel() {
    return android.os.Build.VERSION.SDK_INT;
}
import * as commonModule from "./common";
export * from "./common";
Builder.knownTemplates.add('suggestionItemTemplate');
export class TokenModel extends commonModule.TokenModel {
    constructor(text, imageName) {
        super(text, imageName);
        if (imageName !== null && imageName !== undefined) {
            const drawable = RadAutoCompleteTextView.resolveDrawableFromResource(imageName);
            this._android = new com.telerik.widget.autocomplete.TokenModel(text, drawable);
            this._android.setNsImageName(imageName);
        }
        else {
            this._android = new com.telerik.widget.autocomplete.TokenModel(text, null);
        }
    }
    get android() {
        return this._android;
    }
}
function nativeModelToNSModel(nativeItem, items) {
    if (!items) {
        return null;
    }
    for (let i = 0; i < items.length; i++) {
        let tokenModel;
        if (items instanceof ObservableArray) {
            tokenModel = items.getItem(i);
        }
        else {
            tokenModel = items[i];
        }
        if (+tokenModel.android.tokenId() === +nativeItem.tokenId()) {
            return tokenModel;
        }
    }
    return null;
}
export class SuggestionView extends commonModule.SuggestionView {
    constructor() {
        super();
    }
    get android() {
        return this._android;
    }
    createNativeView() {
        return this.owner ? this.getNativeBackgroundView() : super.createNativeView();
    }
    set android(value) {
        this._android = value;
        this.setNativeView(this.getNativeBackgroundView());
    }
    getNativeBackgroundView() {
        return this.owner.android.getPopUp().getContentView();
    }
    [heightProperty.getDefault]() {
        return this.suggestionViewHeight;
    }
    [heightProperty.setNative](value) {
        this.suggestionViewHeight = value;
    }
    onSuggestionViewHeightChanged(oldValue, newValue) {
        this.updateHeight();
    }
    updateHeight() {
        if (this.owner && this.owner.nativeView && this.suggestionViewHeight) {
            const heightInPixels = Utils.layout.toDevicePixels(this.suggestionViewHeight);
            this.owner.nativeView.setSuggestionViewHeight(heightInPixels);
        }
    }
}
let AutoCompleteAdapterClass;
function ensureAutoCompleteAdapter() {
    if (AutoCompleteAdapterClass) {
        return AutoCompleteAdapterClass;
    }
    var AutoCompleteAdapter = /** @class */ (function (_super) {
    __extends(AutoCompleteAdapter, _super);
    function AutoCompleteAdapter(owner, items, isRemote) {
        var _this = _super.call(this, Utils.android.getApplicationContext(), items, null) || this;
        _this.suggestionsMap = {};
        _this.owner = owner;
        _this.isRemote = isRemote;
        return global.__native(_this);
    }
    AutoCompleteAdapter.prototype.onCreateViewHolder = function (parent, viewType) {
        var view = this.owner.getViewForViewType(commonModule.AutoCompleteViewType.ItemView);
        var parentView = new StackLayout();
        parentView.orientation = "vertical";
        commonModule.setupCssScope(this.owner, view);
        parentView.addChild(view);
        this.owner.suggestionView._addView(parentView);
        var layoutParams = new org.nativescript.widgets.CommonLayoutParams();
        layoutParams.width = org.nativescript.widgets.CommonLayoutParams.MATCH_PARENT;
        layoutParams.height = org.nativescript.widgets.CommonLayoutParams.WRAP_CONTENT;
        var holder = new com.telerik.widget.autocomplete.SuggestionItemViewHolder(parentView.android);
        parentView.android.setLayoutParams(layoutParams);
        holder['nsView'] = parentView;
        return holder;
    };
    AutoCompleteAdapter.prototype.onBindViewHolder = function (holder, position) {
        var nativeItem = this.getFilteredList().get(position);
        var model = nativeModelToNSModel(nativeItem, this.owner.filteredItems);
        if (holder.nsView !== undefined) {
            holder.nsView.bindingContext = model;
            this.attachOnClickListener(holder.itemView);
            var args = {
                eventName: commonModule.RadAutoCompleteTextView.itemLoadingEvent,
                index: position,
                view: holder.nsView._subViews[0],
                android: holder,
                data: model
            };
            this.owner.notify(args);
        }
    };
    AutoCompleteAdapter.prototype.filter = function (charText) {
        if (!this.isRemote) {
            _super.prototype.filter.call(this, charText);
            var nsResult = new Array();
            for (var i = 0; i < this.getFilteredList().size(); i++) {
                var nativeItem = this.getFilteredList().get(i);
                var model = nativeModelToNSModel(nativeItem, this.owner.items);
                nsResult.push(model);
            }
            this.owner._filteredItems = nsResult;
            return;
        }
        this.owner.android.getAdapter().getFilteredList().clear();
        charText = charText.toLowerCase();
        var self = this;
        if (charText.length !== 0) {
            this.owner.asyncCall(charText).then(function (items) {
                var result = new java.util.ArrayList();
                var nsResult = new Array();
                if (self.owner.completionMode === commonModule.AutoCompleteCompletionMode.StartsWith) {
                    for (var i = 0; i < items.length; i++) {
                        var current = items[i];
                        var upperCase = current.android.getText().toLowerCase();
                        if (upperCase.startsWith(charText)) {
                            result.add(current.android);
                            nsResult.push(current);
                        }
                    }
                }
                else {
                    for (var i = 0; i < items.length; i++) {
                        var current = items[i];
                        var upperCase = current.android.getText().toLowerCase();
                        if (upperCase.indexOf(charText !== -1)) {
                            result.add(current.android);
                            nsResult.push(current);
                        }
                    }
                }
                if (self.owner.isLoaded) {
                    self.setItems(result);
                    self.owner.android.getAdapter().setFilteredList(result);
                    self.owner.android.resolveAfterFilter(self.owner.android.getTextField().getText().toString(), true);
                    self.owner._filteredItems = nsResult;
                }
            });
        }
    };
    return AutoCompleteAdapter;
}(com.telerik.widget.autocomplete.AutoCompleteAdapter));
    AutoCompleteAdapterClass = AutoCompleteAdapter;
}
let AutoCompleteShowSuggestionListener;
let AutoCompleteOnFocusChangeListener;
let AutoCompleteTextChangedListener;
let AutoCompleteTokenAddedAtIndexListener;
let AutoCompleteTokenSelectedListener;
let AutoCompleteTokenDeselectedListener;
let AutoCompleteTokenRemovedListener;
let AutoCompleteDidAutoCompleteTokenListener;
function initListeners() {
    if (!AutoCompleteShowSuggestionListener) {
        var AutoCompleteShowSuggestionListenerImpl = /** @class */ (function (_super) {
    __extends(AutoCompleteShowSuggestionListenerImpl, _super);
    function AutoCompleteShowSuggestionListenerImpl(owner) {
        var _this = _super.call(this) || this;
        _this.owner = owner;
        return global.__native(_this);
    }
    AutoCompleteShowSuggestionListenerImpl.prototype.onShowSuggestionList = function (autoComplete, suggestions) {
        var args = new commonModule.AutoCompleteEventData(this.owner, commonModule.RadAutoCompleteTextView.suggestionViewBecameVisibleEvent, undefined, undefined);
        this.owner.notify(args);
    };
    AutoCompleteShowSuggestionListenerImpl = __decorate([
        Interfaces([com.telerik.widget.autocomplete.ShowSuggestionListListener])
    ], AutoCompleteShowSuggestionListenerImpl);
    return AutoCompleteShowSuggestionListenerImpl;
}(java.lang.Object));
        AutoCompleteShowSuggestionListener = AutoCompleteShowSuggestionListenerImpl;
    }
    if (!AutoCompleteOnFocusChangeListener) {
        var AutoCompleteOnFocusChangeListenerImpl = /** @class */ (function (_super) {
    __extends(AutoCompleteOnFocusChangeListenerImpl, _super);
    function AutoCompleteOnFocusChangeListenerImpl(owner) {
        var _this = _super.call(this) || this;
        _this.owner = owner;
        return global.__native(_this);
    }
    AutoCompleteOnFocusChangeListenerImpl.prototype.onFocusChange = function (view, hasFocus) {
        if (hasFocus) {
            this.owner.notify({ eventName: EditableTextBase.focusEvent, object: this.owner });
        }
        else {
            this.owner.notify({ eventName: EditableTextBase.blurEvent, object: this.owner });
        }
    };
    AutoCompleteOnFocusChangeListenerImpl = __decorate([
        Interfaces([android.view.View.OnFocusChangeListener])
    ], AutoCompleteOnFocusChangeListenerImpl);
    return AutoCompleteOnFocusChangeListenerImpl;
}(java.lang.Object));
        AutoCompleteOnFocusChangeListener = AutoCompleteOnFocusChangeListenerImpl;
    }
    if (!AutoCompleteTextChangedListener) {
        var AutoCompleteTextChangedListenerImpl = /** @class */ (function (_super) {
    __extends(AutoCompleteTextChangedListenerImpl, _super);
    function AutoCompleteTextChangedListenerImpl(owner) {
        var _this = _super.call(this) || this;
        _this.owner = owner;
        return global.__native(_this);
    }
    AutoCompleteTextChangedListenerImpl.prototype.onTextChanged = function (autoComplete, text) {
        if (this.owner.text !== text) {
            var args = new commonModule.AutoCompleteEventData(this.owner, commonModule.RadAutoCompleteTextView.textChangedEvent, text, undefined);
            this.owner.text = text;
            this.owner.notify(args);
        }
    };
    AutoCompleteTextChangedListenerImpl = __decorate([
        Interfaces([com.telerik.widget.autocomplete.TextChangedListener])
    ], AutoCompleteTextChangedListenerImpl);
    return AutoCompleteTextChangedListenerImpl;
}(java.lang.Object));
        AutoCompleteTextChangedListener = AutoCompleteTextChangedListenerImpl;
    }
    if (!AutoCompleteTokenAddedAtIndexListener) {
        var AutoCompleteTokenAddedAtIndexListenerImpl = /** @class */ (function (_super) {
    __extends(AutoCompleteTokenAddedAtIndexListenerImpl, _super);
    function AutoCompleteTokenAddedAtIndexListenerImpl(owner) {
        var _this = _super.call(this) || this;
        _this.owner = owner;
        return global.__native(_this);
    }
    AutoCompleteTokenAddedAtIndexListenerImpl.prototype.onTokenAddedAtIndex = function (autoComplete, token, index) {
        var tokenModel = nativeModelToNSModel(token, this.owner.filteredItems);
        var nativeTokenView = this.owner.android.getTokenAt(index);
        var tokenView = new Token(nativeTokenView);
        this.owner._tokens.push(tokenView);
        commonModule.setupCssScope(this.owner, tokenView);
        commonModule.setupCssScope(this.owner, tokenView.clearButton);
        this.owner._addView(tokenView);
        this.owner._onCssStateChange();
        this.owner._selectedTokens.push(tokenModel);
        var args = new commonModule.AutoCompleteEventData(this.owner, commonModule.RadAutoCompleteTextView.tokenAddedEvent, token.getText(), tokenModel);
        this.owner.notify(args);
    };
    AutoCompleteTokenAddedAtIndexListenerImpl = __decorate([
        Interfaces([com.telerik.widget.autocomplete.TokenAddedAtIndexListener])
    ], AutoCompleteTokenAddedAtIndexListenerImpl);
    return AutoCompleteTokenAddedAtIndexListenerImpl;
}(java.lang.Object));
        AutoCompleteTokenAddedAtIndexListener = AutoCompleteTokenAddedAtIndexListenerImpl;
    }
    if (!AutoCompleteTokenSelectedListener) {
        var AutoCompleteTokenSelectedListenerImpl = /** @class */ (function (_super) {
    __extends(AutoCompleteTokenSelectedListenerImpl, _super);
    function AutoCompleteTokenSelectedListenerImpl(owner) {
        var _this = _super.call(this) || this;
        _this.owner = owner;
        return global.__native(_this);
    }
    AutoCompleteTokenSelectedListenerImpl.prototype.onTokenSelected = function (autoComplete, token, tokenView) {
        var tokenModel = nativeModelToNSModel(token, this.owner._selectedTokens);
        var args = new commonModule.AutoCompleteEventData(this.owner, commonModule.RadAutoCompleteTextView.tokenSelectedEvent, token.getText(), tokenModel);
        this.owner.notify(args);
        for (var i = 0; i < this.owner._tokens.length; i++) {
            if (this.owner._tokens[i].android === tokenView) {
                this.owner._tokens[i]._goToVisualState("selected");
            }
        }
    };
    AutoCompleteTokenSelectedListenerImpl = __decorate([
        Interfaces([com.telerik.widget.autocomplete.TokenSelectedListener])
    ], AutoCompleteTokenSelectedListenerImpl);
    return AutoCompleteTokenSelectedListenerImpl;
}(java.lang.Object));
        AutoCompleteTokenSelectedListener = AutoCompleteTokenSelectedListenerImpl;
    }
    if (!AutoCompleteTokenDeselectedListener) {
        var AutoCompleteTokenDeselectedListenerImpl = /** @class */ (function (_super) {
    __extends(AutoCompleteTokenDeselectedListenerImpl, _super);
    function AutoCompleteTokenDeselectedListenerImpl(owner) {
        var _this = _super.call(this) || this;
        _this.owner = owner;
        return global.__native(_this);
    }
    AutoCompleteTokenDeselectedListenerImpl.prototype.onTokenDeselected = function (autoComplete, token, tokenView) {
        var tokenModel = nativeModelToNSModel(token, this.owner._selectedTokens);
        var args = new commonModule.AutoCompleteEventData(this.owner, commonModule.RadAutoCompleteTextView.tokenDeselectedEvent, token.getText(), tokenModel);
        this.owner.notify(args);
        for (var i = 0; i < this.owner._tokens.length; i++) {
            if (this.owner._tokens[i].android === tokenView) {
                this.owner._tokens[i]._goToVisualState("normal");
            }
        }
    };
    AutoCompleteTokenDeselectedListenerImpl = __decorate([
        Interfaces([com.telerik.widget.autocomplete.TokenDeselectedListener])
    ], AutoCompleteTokenDeselectedListenerImpl);
    return AutoCompleteTokenDeselectedListenerImpl;
}(java.lang.Object));
        AutoCompleteTokenDeselectedListener = AutoCompleteTokenDeselectedListenerImpl;
    }
    if (!AutoCompleteTokenRemovedListener) {
        var AutoCompleteTokenRemovedListenerImpl = /** @class */ (function (_super) {
    __extends(AutoCompleteTokenRemovedListenerImpl, _super);
    function AutoCompleteTokenRemovedListenerImpl(owner) {
        var _this = _super.call(this) || this;
        _this.owner = owner;
        return global.__native(_this);
    }
    AutoCompleteTokenRemovedListenerImpl.prototype.onTokenRemoved = function (autoComplete, token) {
        var tokenModel = nativeModelToNSModel(token, this.owner._selectedTokens);
        var indexToRemove = this.owner._selectedTokens.indexOf(tokenModel);
        if (indexToRemove >= 0) {
            this.owner._selectedTokens.splice(indexToRemove, 1);
        }
        var args = new commonModule.AutoCompleteEventData(this.owner, commonModule.RadAutoCompleteTextView.tokenRemovedEvent, token.getText(), tokenModel);
        this.owner.notify(args);
    };
    AutoCompleteTokenRemovedListenerImpl = __decorate([
        Interfaces([com.telerik.widget.autocomplete.TokenRemovedListener])
    ], AutoCompleteTokenRemovedListenerImpl);
    return AutoCompleteTokenRemovedListenerImpl;
}(java.lang.Object));
        AutoCompleteTokenRemovedListener = AutoCompleteTokenRemovedListenerImpl;
    }
    if (!AutoCompleteDidAutoCompleteTokenListener) {
        var AutoCompleteDidAutoCompleteTokenListenerImpl = /** @class */ (function (_super) {
    __extends(AutoCompleteDidAutoCompleteTokenListenerImpl, _super);
    function AutoCompleteDidAutoCompleteTokenListenerImpl(owner) {
        var _this = _super.call(this) || this;
        _this.owner = owner;
        return global.__native(_this);
    }
    AutoCompleteDidAutoCompleteTokenListenerImpl.prototype.onDidAutoComplete = function (autoComplete, token) {
        var tokenModel = nativeModelToNSModel(token, this.owner.filteredItems);
        var args = new commonModule.AutoCompleteEventData(this.owner, commonModule.RadAutoCompleteTextView.didAutoCompleteEvent, token.getText(), tokenModel);
        this.owner.notify(args);
    };
    AutoCompleteDidAutoCompleteTokenListenerImpl = __decorate([
        Interfaces([com.telerik.widget.autocomplete.DidAutoCompleteTokenListener])
    ], AutoCompleteDidAutoCompleteTokenListenerImpl);
    return AutoCompleteDidAutoCompleteTokenListenerImpl;
}(java.lang.Object));
        AutoCompleteDidAutoCompleteTokenListener = AutoCompleteDidAutoCompleteTokenListenerImpl;
    }
}
export class RadAutoCompleteTextView extends commonModule.RadAutoCompleteTextView {
    constructor() {
        super();
        this._selectedTokens = new Array();
        this._androidViewId = -1;
    }
    get clearButton() {
        return this._clearButton;
    }
    get noResultsLabel() {
        return this._noResultsLabel;
    }
    createNativeView() {
        this._android = new com.telerik.widget.autocomplete.RadAutoCompleteTextView(this._context);
        initListeners();
        this._android._showSuggestionListener = new AutoCompleteShowSuggestionListener(this);
        this._android._onFocusChangeListener = new AutoCompleteOnFocusChangeListener(this);
        this._android._textChangedListener = new AutoCompleteTextChangedListener(this);
        this._android._tokenAddedAtIndexListener = new AutoCompleteTokenAddedAtIndexListener(this);
        this._android._tokenSelectedListener = new AutoCompleteTokenSelectedListener(this);
        this._android._tokenDeselectedListener = new AutoCompleteTokenDeselectedListener(this);
        this._android._tokenRemovedListener = new AutoCompleteTokenRemovedListener(this);
        this._android._didAutoCompleteTokenListener = new AutoCompleteDidAutoCompleteTokenListener(this);
        this._android.addShowSuggestionListListener(this._android._showSuggestionListener);
        this._android.addOnFocusChangeListener(this._android._onFocusChangeListener);
        this._android.addTextChangedListener(this._android._textChangedListener);
        this._android.addTokenAddedAtIndexListener(this._android._tokenAddedAtIndexListener);
        this._android.addTokenSelectedListener(this._android._tokenSelectedListener);
        this._android.addTokenDeselectedListener(this._android._tokenDeselectedListener);
        this._android.addTokenRemovedListener(this._android._tokenRemovedListener);
        this._android.addDidAutoCompleteTokenListener(this._android._didAutoCompleteTokenListener);
        this._ngKey = Object.keys(this).find(key => key.startsWith('_ngcontent'));
        this._vueKey = Object.keys(this).find(key => key.startsWith('data-v'));
        if (this.displayMode) {
            this.adjustDisplayMode(this.displayMode);
        }
        if (this.suggestMode) {
            this.adjustSuggestMode(this.suggestMode);
        }
        if (this.layoutMode) {
            this.adjustLayoutMode(this.layoutMode);
        }
        if (this.completionMode) {
            this.adjustCompletionMode(this.completionMode);
        }
        if (this.suggestionView) {
            this.adjustSuggestionView(this.suggestionView);
        }
        else {
            this.suggestionView = new SuggestionView();
        }
        if (this.hint) {
            this.adjustHint(this.hint);
        }
        if (this.text) {
            this.adjustText(this.text);
        }
        if (this.minimumCharactersToSearch) {
            this.adjustMinimumCharactersToSearch(this.minimumCharactersToSearch);
        }
        if (this.tokenItemSpacing) {
            this.adjustTokenItemSpacing(this.tokenItemSpacing);
        }
        if (this.noResultsText) {
            this.adjustNoResultsText(this.noResultsText);
        }
        if (this.closeButtonImageSrc) {
            this.adjustCloseButtonImage(this.closeButtonImageSrc);
        }
        this.adjustShowCloseButton(this.showCloseButton);
        if (this.readOnly !== undefined) {
            this.adjustIsReadOnly(this.readOnly);
        }
        if (this.asyncCall) {
            this.adjustAsyncCall(true);
        }
        else {
            this.loadData(false);
        }
        this._clearButton = new ClearButton(this._android);
        commonModule.setupCssScope(this, this._clearButton);
        this._addView(this._clearButton);
        this._noResultsLabel = new NoResultsLabel(this._android.noResultsLabel());
        commonModule.setupCssScope(this, this._noResultsLabel);
        this._addView(this._noResultsLabel);
        return this._nativeView;
    }
    initNativeView() {
        super.initNativeView();
        if (this._androidViewId < 0) {
            this._androidViewId = android.view.View.generateViewId();
        }
        this._nativeView.setId(this._androidViewId);
    }
    disposeNativeView() {
        this._android.resetAutocomplete();
        if (this.clearButton) {
            this._removeView(this.clearButton);
        }
        if (this._android._showSuggestionListener) {
            this._android._showSuggestionListener.owner = null;
        }
        if (this._android._onFocusChangeListener) {
            this._android._onFocusChangeListener.owner = null;
        }
        if (this._android._textChangedListener) {
            this._android._textChangedListener.owner = null;
        }
        if (this._android._tokenAddedAtIndexListener) {
            this._android._tokenAddedAtIndexListener.owner = null;
        }
        if (this._android._tokenSelectedListener) {
            this._android._tokenSelectedListener.owner = null;
        }
        if (this._android._tokenDeselectedListener) {
            this._android._tokenDeselectedListener.owner = null;
        }
        if (this._android._tokenRemovedListener) {
            this._android._tokenRemovedListener.owner = null;
        }
        if (this._android._didAutoCompleteTokenListener) {
            this._android._didAutoCompleteTokenListener.owner = null;
        }
        super.disposeNativeView();
    }
    get _nativeView() {
        return this._android;
    }
    // @ts-ignore
    get android() {
        return this._android;
    }
    get filteredItems() {
        if (this._filteredItems === undefined) {
            this._filteredItems = new Array();
        }
        return this._filteredItems;
    }
    get selectedTokens() {
        return this._selectedTokens;
    }
    resetAutoComplete() {
        this._android.resetAutocomplete();
    }
    addToken(token) {
        if (!token) {
            return;
        }
        this.filteredItems.push(token);
        this._android.addTokenView(token.android);
        if (this._android.getDisplayMode() === com.telerik.widget.autocomplete.DisplayMode.PLAIN) {
            // HACK: to unify behavior in iOS and Android
            this._android.setText(token.text);
        }
    }
    insertTokenAtIndex(token, index) {
        if (!token) {
            return;
        }
        this.filteredItems.push(token);
        this._android.addTokenModelAt(token.android, index);
    }
    removeToken(token) {
        if (!token) {
            return;
        }
        this._android.removeTokenModel(token.android);
    }
    removeTokenAtIndex(index) {
        this._android.removeTokenAt(index);
    }
    removeAllTokens() {
        this._android.removeAllTokens();
    }
    tokens() {
        return this._android.getTokens();
    }
    tokenAtIndex(index) {
        return this._android.getTokenAt(index);
    }
    findSuggestions() {
        return this._android.findSuggestions();
    }
    onDisplayModeChanged(oldValue, newValue) {
        this.adjustDisplayMode(newValue);
    }
    onLayoutModeChanged(oldValue, newValue) {
        this.adjustLayoutMode(newValue);
    }
    onSuggestModeChanged(oldValue, newValue) {
        this.adjustSuggestMode(newValue);
    }
    onCompletionModeChanged(oldValue, newValue) {
        this.adjustCompletionMode(newValue);
    }
    onItemsChanged(oldValue, newValue) {
        this.loadData(false);
    }
    onSuggestionViewChanged(oldValue, newValue) {
        if (oldValue && oldValue.parent === this) {
            this._removeView(oldValue);
        }
        this.adjustSuggestionView(newValue);
    }
    onHintChanged(oldValue, newValue) {
        this.adjustHint(newValue);
    }
    onTextChanged(oldValue, newValue) {
        this.adjustText(newValue);
    }
    onMinimumCharactersToSearchChanged(oldValue, newValue) {
        this.adjustMinimumCharactersToSearch(newValue);
    }
    onTokenItemSpacingChanged(oldValue, newValue) {
        this.adjustTokenItemSpacing(newValue);
    }
    onNoResultsTextChanged(oldValue, newValue) {
        this.adjustNoResultsText(newValue);
    }
    onLoadSuggestionsAsyncChanged(oldValue, newValue) {
        this.asyncCall = newValue;
        this.adjustAsyncCall(true);
    }
    onShowCloseButtonChanged(oldValue, newValue) {
        this.adjustShowCloseButton(newValue);
    }
    onCloseButtonImageSrcChanged(oldValue, newValue) {
        this.adjustCloseButtonImage(newValue);
    }
    onReadOnlyChanged(oldValue, newValue) {
        this.adjustIsReadOnly(newValue);
    }
    adjustAsyncCall(value) {
        if (this._android && value) {
            this._android.setUsingAsyncData(value);
            this.loadData(true);
        }
    }
    adjustHint(value) {
        if (this._android && value) {
            this._android.setAutocompleteHint(value);
        }
    }
    adjustText(value) {
        if (this._android && this._android.getText().toString() !== value) {
            this._android.setText(value);
            const args = new commonModule.AutoCompleteEventData(this, commonModule.RadAutoCompleteTextView.textChangedEvent, value, undefined);
            this.notify(args);
        }
    }
    adjustMinimumCharactersToSearch(value) {
        if (this._android && value) {
            this._android.setMinimumCharactersToSearch(value);
        }
    }
    adjustTokenItemSpacing(value) {
        if (this._android && value) {
            const spacingInPixels = Utils.layout.toDevicePixels(value);
            this._android.setTokenItemSpacing(spacingInPixels);
        }
    }
    adjustNoResultsText(value) {
        if (this._android && value) {
            this._android.setNoResultsText(value);
        }
    }
    adjustSuggestionView(value) {
        if (this._android && value) {
            const suggestionView = value;
            suggestionView.owner = this;
            suggestionView.android = this._android.getSuggestionView();
            if (suggestionView.suggestionViewHeight) {
                const heightInPixels = Utils.layout.toDevicePixels(suggestionView.suggestionViewHeight);
                this._android.setSuggestionViewHeight(heightInPixels);
            }
            commonModule.setupCssScope(this, suggestionView);
            this._addView(suggestionView);
        }
    }
    adjustCompletionMode(value) {
        if (this._android && value) {
            if (value === commonModule.AutoCompleteCompletionMode.Contains) {
                this._android.getAdapter().setCompletionMode(com.telerik.widget.autocomplete.CompletionMode.CONTAINS);
            }
            else {
                this._android.getAdapter().setCompletionMode(com.telerik.widget.autocomplete.CompletionMode.STARTS_WITH);
            }
        }
    }
    adjustDisplayMode(value) {
        if (this._android && value) {
            this._android.setDisplayMode((value === commonModule.AutoCompleteDisplayMode.Plain) ?
                com.telerik.widget.autocomplete.DisplayMode.PLAIN :
                com.telerik.widget.autocomplete.DisplayMode.TOKENS);
        }
    }
    adjustSuggestMode(value) {
        if (this._android && value) {
            if (value === commonModule.AutoCompleteSuggestMode.Suggest) {
                this._android.setSuggestMode(com.telerik.widget.autocomplete.SuggestMode.SUGGEST);
            }
            else if (value === commonModule.AutoCompleteSuggestMode.Append) {
                this._android.setSuggestMode(com.telerik.widget.autocomplete.SuggestMode.APPEND);
            }
            else {
                this._android.setSuggestMode(com.telerik.widget.autocomplete.SuggestMode.SUGGEST_APPEND);
            }
        }
    }
    adjustLayoutMode(value) {
        if (value && this._android) {
            this._android.setTokensLayoutMode((value === commonModule.AutoCompleteLayoutMode.Horizontal) ?
                com.telerik.widget.autocomplete.LayoutMode.HORIZONTAL :
                com.telerik.widget.autocomplete.LayoutMode.WRAP);
        }
    }
    adjustShowCloseButton(value) {
        if (this._android) {
            this._android.setShowCloseButton(value);
        }
    }
    adjustCloseButtonImage(value) {
        if (this._android) {
            if (value !== null && value !== undefined) {
                const drawable = RadAutoCompleteTextView.resolveDrawableFromResource(value);
                if (drawable) {
                    this._android.getCloseButtonView().setBackgroundDrawable(drawable);
                }
            }
        }
    }
    adjustIsReadOnly(value) {
        if (this._android) {
            this._android.setReadOnly(value);
        }
    }
    loadData(isRemote) {
        if (this._nativeView !== undefined) {
            if ((this.items === undefined && isRemote === true) || this.items !== undefined) {
                const nativeSource = new java.util.ArrayList();
                if (this.items !== undefined) {
                    for (let i = 0; i < this.items.length; i++) {
                        const a = this.items.getItem(i);
                        nativeSource.add(a.android);
                    }
                }
                ensureAutoCompleteAdapter();
                const adapter = new AutoCompleteAdapterClass(this, nativeSource, isRemote);
                this._android.setAdapter(adapter);
                this.adjustCompletionMode(this.completionMode);
            }
        }
    }
    static resolveDrawableFromResource(imageName) {
        let nativeValue = imageName;
        let drawable;
        if (imageName.startsWith("res://")) {
            nativeValue = imageName.substring(6, imageName.length);
        }
        const appResources = Utils.android.getApplicationContext().getResources();
        const packageName = Utils.android.getApplication().getPackageName();
        if (appResources) {
            const identifier = appResources.getIdentifier(nativeValue, 'drawable', packageName);
            if (identifier > 0) {
                drawable = appResources.getDrawable(identifier);
            }
            else {
                console.log("WARNING: Can't find drawable with name: " + nativeValue);
            }
        }
        return drawable;
    }
    [colorProperty.getDefault]() {
        return this._android.getTextField().getCurrentTextColor();
    }
    [colorProperty.setNative](value) {
        const nativeColor = value instanceof Color ? value.android : value;
        if (!nativeColor) {
            this._android.getTextField().setTextColor(android.graphics.Color.BLACK);
            this._android.setAppendTextColor(android.graphics.Color.GRAY);
        }
        else {
            this._android.getTextField().setTextColor(nativeColor);
            this._android.setAppendTextColor(androidColorWithAlpha(nativeColor, 0.5));
        }
    }
    [fontSizeProperty.getDefault]() {
        return this._android.getTextField().getTextSize();
    }
    [fontSizeProperty.setNative](value) {
        if (!value) {
            this._android.getTextField().setTextSize(android.util.TypedValue.COMPLEX_UNIT_SP, 14);
        }
        if (typeof value === "number") {
            this._android.getTextField().setTextSize(value);
        }
        else {
            this._android.getTextField().setTextSize(android.util.TypedValue.COMPLEX_UNIT_PX, value.nativeSize);
        }
    }
    [fontInternalProperty.getDefault]() {
        return this._android.getTextField().getTypeface();
    }
    [fontInternalProperty.setNative](value) {
        this._android.getTextField().setTypeface(value instanceof Font ? value.getAndroidTypeface() : value);
    }
    [paddingLeftProperty.getDefault]() {
        return this._android.getTextField().getPaddingLeft();
    }
    // RadAutoCompleteTextView has default paddingBottom and its
    // EditText has default paddingTop and paddingBottom. When the
    // padding is changed through NativeScript, we reset any default values,
    // so the value that is set is the only padding that is applied.
    [paddingLeftProperty.setNative](value) {
        const paddingLeft = Utils.layout.toDevicePixels(value) + this.effectiveBorderLeftWidth;
        this._android.setCustomPaddingLeft(paddingLeft);
        const textTop = this._android.getTextField().getPaddingTop();
        const textRight = this._android.getTextField().getPaddingRight();
        const textBottom = this._android.getTextField().getPaddingBottom();
        this._android.getTextField().setPadding(0, textTop, textRight, textBottom);
    }
    [borderLeftWidthProperty.getDefault]() {
        return 0;
    }
    [borderLeftWidthProperty.setNative](value) {
        let paddingLeft = Utils.layout.toDevicePixels(value) + this.effectivePaddingLeft;
        this._android.setCustomPaddingLeft(paddingLeft);
    }
    [paddingTopProperty.getDefault]() {
        return this._android.getTextField().getPaddingTop();
    }
    [paddingTopProperty.setNative](value) {
        const paddingTop = Utils.layout.toDevicePixels(value) + this.effectiveBorderTopWidth;
        this._android.setCustomPaddingTop(paddingTop);
        const textLeft = this._android.getTextField().getPaddingLeft();
        const textRight = this._android.getTextField().getPaddingRight();
        const textBottom = this._android.getTextField().getPaddingBottom();
        this._android.getTextField().setPadding(textLeft, 0, textRight, textBottom);
    }
    [borderTopWidthProperty.getDefault]() {
        return 0;
    }
    [borderTopWidthProperty.setNative](value) {
        let paddingTop = Utils.layout.toDevicePixels(value) + this.effectivePaddingTop;
        this._android.setCustomPaddingTop(paddingTop);
    }
    [paddingRightProperty.getDefault]() {
        return this._android.getTextField().getPaddingRight();
    }
    [paddingRightProperty.setNative](value) {
        const paddingRight = Utils.layout.toDevicePixels(value) + this.effectiveBorderRightWidth;
        this._android.setCustomPaddingRight(paddingRight);
        const textLeft = this._android.getTextField().getPaddingLeft();
        const textTop = this._android.getTextField().getPaddingTop();
        const textBottom = this._android.getTextField().getPaddingBottom();
        this._android.getTextField().setPadding(textLeft, textTop, 0, textBottom);
    }
    [borderRightWidthProperty.getDefault]() {
        return 0;
    }
    [borderRightWidthProperty.setNative](value) {
        let paddingRight = Utils.layout.toDevicePixels(value) + this.effectivePaddingRight;
        this._android.setCustomPaddingRight(paddingRight);
    }
    [paddingBottomProperty.getDefault]() {
        return this._android.getPaddingBottom();
    }
    [paddingBottomProperty.setNative](value) {
        const paddingLeft = this._android.getPaddingLeft();
        const paddingTop = this._android.getPaddingTop();
        const paddingRight = this._android.getPaddingRight();
        const paddingBottom = Utils.layout.toDevicePixels(value) + this.effectiveBorderBottomWidth;
        this._android.setPadding(paddingLeft, paddingTop, paddingRight, 0);
        this._android.setCustomPaddingBottom(paddingBottom);
        const textLeft = this._android.getTextField().getPaddingLeft();
        const textTop = this._android.getTextField().getPaddingTop();
        const textRight = this._android.getTextField().getPaddingRight();
        this._android.getTextField().setPadding(textLeft, textTop, textRight, 0);
    }
    [borderBottomWidthProperty.getDefault]() {
        return 0;
    }
    [borderBottomWidthProperty.setNative](value) {
        let paddingBottom = Utils.layout.toDevicePixels(value) + this.effectivePaddingBottom;
        this._android.setCustomPaddingBottom(paddingBottom);
    }
}
function androidColorWithAlpha(androidColor, alpha) {
    const colorRed = android.graphics.Color.red(androidColor);
    const colorGreen = android.graphics.Color.green(androidColor);
    const colorBlue = android.graphics.Color.blue(androidColor);
    return android.graphics.Color.argb(alpha * 256.0, colorRed, colorGreen, colorBlue);
}
export class Token extends commonModule.Token {
    constructor(nativeView) {
        super();
        this._android = nativeView;
        this._clearButton = new TokenClearButton(this._android);
        this._addView(this._clearButton);
    }
    createNativeView() {
        return this._android;
    }
    disposeNativeView() {
        if (this._clearButton) {
            this._removeView(this._clearButton);
        }
        this._android = null;
    }
    get clearButton() {
        return this._clearButton;
    }
    eachChild(callback) {
        if (this.clearButton) {
            callback(this.clearButton);
        }
    }
    [colorProperty.getDefault]() {
        return this._android.textView().getCurrentTextColor();
    }
    [colorProperty.setNative](value) {
        const nsColor = value instanceof Color ? value.android : value;
        if (nsColor) {
            this._android.textView().setTextColor(nsColor);
        }
        else {
            this._android.textView().setTextColor(android.graphics.Color.BLACK);
        }
    }
    [fontSizeProperty.getDefault]() {
        return this._android.textView().getTextSize();
    }
    [fontSizeProperty.setNative](value) {
        if (!value) {
            this._android.textView().setTextSize(android.util.TypedValue.COMPLEX_UNIT_SP, 14);
        }
        if (typeof value === "number") {
            this._android.textView().setTextSize(value);
        }
        else {
            this._android.textView().setTextSize(android.util.TypedValue.COMPLEX_UNIT_PX, value.nativeSize);
        }
    }
    [fontInternalProperty.getDefault]() {
        return this._android.textView().getTypeface();
    }
    [fontInternalProperty.setNative](value) {
        this._android.textView().setTypeface(value instanceof Font ? value.getAndroidTypeface() : value);
    }
    [paddingLeftProperty.getDefault]() {
        return this._android.getPaddingLeft();
    }
    [paddingLeftProperty.setNative](value) {
        const paddingTop = this._android.getPaddingTop();
        const paddingRight = this._android.getPaddingRight();
        const paddingBottom = this._android.getPaddingBottom();
        this._android.setPadding(value, paddingTop, paddingRight, paddingBottom);
    }
    [paddingTopProperty.getDefault]() {
        return this._android.getPaddingTop();
    }
    [paddingTopProperty.setNative](value) {
        const paddingLeft = this._android.getPaddingLeft();
        const paddingRight = this._android.getPaddingRight();
        const paddingBottom = this._android.getPaddingBottom();
        this._android.setPadding(paddingLeft, value, paddingRight, paddingBottom);
    }
    [paddingRightProperty.getDefault]() {
        return this._android.getPaddingRight();
    }
    [paddingRightProperty.setNative](value) {
        const paddingLeft = this._android.getPaddingLeft();
        const paddingTop = this._android.getPaddingTop();
        const paddingBottom = this._android.getPaddingBottom();
        this._android.setPadding(paddingLeft, paddingTop, value, paddingBottom);
    }
    [paddingBottomProperty.getDefault]() {
        return this._android.getPaddingBottom();
    }
    [paddingBottomProperty.setNative](value) {
        const paddingLeft = this._android.getPaddingLeft();
        const paddingTop = this._android.getPaddingTop();
        const paddingRight = this._android.getPaddingRight();
        this._android.setPadding(paddingLeft, paddingTop, paddingRight, value);
    }
}
export class NoResultsLabel extends commonModule.NoResultsLabel {
    constructor(nativeView) {
        super();
        this._android = nativeView;
    }
    createNativeView() {
        return this._android;
    }
    disposeNativeView() {
        this._android = null;
    }
    [colorProperty.getDefault]() {
        return this._android.getCurrentTextColor();
    }
    [colorProperty.setNative](value) {
        const nativeColor = value instanceof Color ? value.android : value;
        if (nativeColor) {
            this._android.setTextColor(nativeColor);
        }
        else {
            this._android.setTextColor(android.graphics.Color.BLACK);
        }
    }
    [fontSizeProperty.getDefault]() {
        return this._android.getTextSize();
    }
    [fontSizeProperty.setNative](value) {
        if (!value) {
            this._android.setTextSize(android.util.TypedValue.COMPLEX_UNIT_SP, 14);
        }
        if (typeof value === "number") {
            this._android.setTextSize(value);
        }
        else {
            this._android.setTextSize(android.util.TypedValue.COMPLEX_UNIT_PX, value.nativeSize);
        }
    }
    [fontInternalProperty.getDefault]() {
        return this._android.getTypeface();
    }
    [fontInternalProperty.setNative](value) {
        this._android.setTypeface(value instanceof Font ? value.getAndroidTypeface() : value);
    }
}
export class ClearButton extends commonModule.ClearButton {
    constructor(nativeView) {
        super();
        this._owner = nativeView;
        this._android = nativeView.getCloseButtonView();
        this.resetStateListAnimator(this._android);
    }
    resetStateListAnimator(view) {
        if (getSdkApiLevel() && getSdkApiLevel() < 21) {
            // Method was introduced with API21, so skip, if earlier.
            return;
        }
        view.setStateListAnimator(null);
    }
    createNativeView() {
        return this._android;
    }
    disposeNativeView() {
        this._android = null;
    }
    [colorProperty.getDefault]() {
        return null;
    }
    [colorProperty.setNative](value) {
        const nativeColor = value instanceof Color ? value.android : value;
        if (nativeColor !== null) {
            const tintList = android.content.res.ColorStateList.valueOf(nativeColor);
            this._owner.makeCloseButtonWhite(true);
            androidx.core.view.ViewCompat.setBackgroundTintList(this._android, tintList);
        }
        else {
            this._owner.makeCloseButtonWhite(false);
            androidx.core.view.ViewCompat.setBackgroundTintList(this._android, null);
        }
    }
    [widthProperty.getDefault]() {
        const widthInPixels = this._owner.getCloseButtonWidth();
        return Utils.layout.toDeviceIndependentPixels(widthInPixels);
    }
    [widthProperty.setNative](value) {
        const widthInPixels = Utils.layout.toDevicePixels(value);
        this._owner.setCloseButtonWidth(widthInPixels);
    }
    [heightProperty.getDefault]() {
        const heightInPixels = this._owner.getCloseButtonHeight();
        return Utils.layout.toDeviceIndependentPixels(heightInPixels);
    }
    [heightProperty.setNative](value) {
        const heightInPixels = Utils.layout.toDevicePixels(value);
        this._owner.setCloseButtonHeight(heightInPixels);
    }
    [paddingLeftProperty.getDefault]() {
        return this._android.getPaddingLeft();
    }
    [paddingLeftProperty.setNative](value) {
        const paddingTop = this._android.getPaddingTop();
        const paddingRight = this._android.getPaddingRight();
        const paddingBottom = this._android.getPaddingBottom();
        this._android.setPadding(value, paddingTop, paddingRight, paddingBottom);
    }
    [paddingTopProperty.getDefault]() {
        return this._android.getPaddingTop();
    }
    [paddingTopProperty.setNative](value) {
        const paddingLeft = this._android.getPaddingLeft();
        const paddingRight = this._android.getPaddingRight();
        const paddingBottom = this._android.getPaddingBottom();
        this._android.setPadding(paddingLeft, value, paddingRight, paddingBottom);
    }
    [paddingRightProperty.getDefault]() {
        return this._android.getPaddingRight();
    }
    [paddingRightProperty.setNative](value) {
        const paddingLeft = this._android.getPaddingLeft();
        const paddingTop = this._android.getPaddingTop();
        const paddingBottom = this._android.getPaddingBottom();
        this._android.setPadding(paddingLeft, paddingTop, value, paddingBottom);
    }
    [paddingBottomProperty.getDefault]() {
        return this._android.getPaddingBottom();
    }
    [paddingBottomProperty.setNative](value) {
        const paddingLeft = this._android.getPaddingLeft();
        const paddingTop = this._android.getPaddingTop();
        const paddingRight = this._android.getPaddingRight();
        this._android.setPadding(paddingLeft, paddingTop, paddingRight, value);
    }
}
export class TokenClearButton extends commonModule.TokenClearButton {
    constructor(token) {
        super();
        this._token = token;
        this._android = token.deleteImage();
    }
    createNativeView() {
        return this._android;
    }
    disposeNativeView() {
        this._android = null;
    }
    [colorProperty.getDefault]() {
        return null;
    }
    [colorProperty.setNative](value) {
        const nativeColor = value instanceof Color ? value.android : value;
        if (nativeColor !== null) {
            const tintList = android.content.res.ColorStateList.valueOf(nativeColor);
            this._token.makeDeleteImageWhite(true);
            androidx.core.widget.ImageViewCompat.setImageTintList(this._android, tintList);
        }
        else {
            this._token.makeDeleteImageWhite(false);
            androidx.core.widget.ImageViewCompat.setImageTintList(this._android, null);
        }
    }
    [backgroundImageProperty.getDefault]() {
        return null;
    }
    [backgroundImageProperty.setNative](value) {
        this._token.setUseDefaultImage(value === null);
    }
    [paddingLeftProperty.getDefault]() {
        return this._android.getPaddingLeft();
    }
    [paddingLeftProperty.setNative](value) {
        const paddingTop = this._android.getPaddingTop();
        const paddingRight = this._android.getPaddingRight();
        const paddingBottom = this._android.getPaddingBottom();
        this._android.setPadding(value, paddingTop, paddingRight, paddingBottom);
    }
    [paddingTopProperty.getDefault]() {
        return this._android.getPaddingTop();
    }
    [paddingTopProperty.setNative](value) {
        const paddingLeft = this._android.getPaddingLeft();
        const paddingRight = this._android.getPaddingRight();
        const paddingBottom = this._android.getPaddingBottom();
        this._android.setPadding(paddingLeft, value, paddingRight, paddingBottom);
    }
    [paddingRightProperty.getDefault]() {
        return this._android.getPaddingRight();
    }
    [paddingRightProperty.setNative](value) {
        const paddingLeft = this._android.getPaddingLeft();
        const paddingTop = this._android.getPaddingTop();
        const paddingBottom = this._android.getPaddingBottom();
        this._android.setPadding(paddingLeft, paddingTop, value, paddingBottom);
    }
    [paddingBottomProperty.getDefault]() {
        return this._android.getPaddingBottom();
    }
    [paddingBottomProperty.setNative](value) {
        const paddingLeft = this._android.getPaddingLeft();
        const paddingTop = this._android.getPaddingTop();
        const paddingRight = this._android.getPaddingRight();
        this._android.setPadding(paddingLeft, paddingTop, paddingRight, value);
    }
}
//# sourceMappingURL=index.android.js.map