import { Builder, View, ObservableArray, PseudoClassHandler, Utils, Color, Font, EditableTextBase, colorProperty, fontInternalProperty, heightProperty, widthProperty, backgroundImageProperty, paddingBottomProperty, paddingTopProperty, paddingLeftProperty, paddingRightProperty, borderLeftWidthProperty, borderTopWidthProperty, borderRightWidthProperty, borderBottomWidthProperty } from "@nativescript/core";
import * as commonModule from "./common";
export * from "./common";
Builder.knownTemplates.add('suggestionItemTemplate');
export class SuggestionView extends commonModule.SuggestionView {
    constructor() {
        super();
    }
    // @ts-ignore
    get ios() {
        return this._ios;
    }
    set ios(value) {
        this._ios = value;
        this.setNativeView(value);
    }
    createNativeView() {
        return this._ios ? this._ios : super.createNativeView();
    }
    updateView() {
        this.updateHeight();
    }
    [heightProperty.getDefault]() {
        return this.suggestionViewHeight;
    }
    [heightProperty.setNative](value) {
        this.suggestionViewHeight = value;
    }
    updateHeight() {
        if (this.owner && this.owner.nativeView && this.suggestionViewHeight) {
            this.owner.nativeView.suggestionViewHeight = this.suggestionViewHeight;
        }
    }
    updateTemplate(value) {
        // TODO: Implement this, probably with Builder same as RadListView templates
        // this._ios.suggestionView = newValue;
        this.updateHeight();
    }
    onSuggestionViewHeightChanged(oldValue, newValue) {
        this.updateHeight();
    }
    onSuggestionItemTemplateChanged(oldValue, newValue) {
        this.updateTemplate(newValue);
    }
}
var SuggestionViewCell = /** @class */ (function (_super) {
    __extends(SuggestionViewCell, _super);
    function SuggestionViewCell() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    SuggestionViewCell.new = function () {
        var instance = _super.new.call(this);
        return instance;
    };
    SuggestionViewCell.class = function () {
        return SuggestionViewCell;
    };
    SuggestionViewCell.prototype.layoutSubviews = function () {
        _super.prototype.layoutSubviews.call(this);
        var itemViewDimensions = this.measureCell(this.view.itemView);
        var cellView = this.view.itemView;
        View.layoutChild(this.owner, cellView, 0, 0, itemViewDimensions.measuredWidth, itemViewDimensions.measuredHeight);
        var measuredHeight = Utils.layout.round(Utils.layout.toDeviceIndependentPixels(itemViewDimensions.measuredHeight));
        this.owner.suggestionView.ios.cellHeight = measuredHeight;
    };
    SuggestionViewCell.prototype.measureCell = function (cellView) {
        if (cellView) {
            var itemWidth = this.owner.getMeasuredWidth();
            var heightSpec = Utils.layout.makeMeasureSpec(0, Utils.layout.UNSPECIFIED);
            var widthSpec = Utils.layout.makeMeasureSpec(itemWidth, Utils.layout.EXACTLY);
            return View.measureChild(this.owner, cellView, widthSpec, heightSpec);
        }
        return undefined;
    };
    return SuggestionViewCell;
}(TKAutoCompleteSuggestionCell));
export class TokenModel extends commonModule.TokenModel {
    constructor(text, image) {
        super(text, image);
        const nativeText = NSString.stringWithCStringEncoding(text, NSUTF8StringEncoding);
        this._ios = TKAutoCompleteToken.alloc().initWithText(nativeText);
        if (image) {
            if (image.startsWith("res://")) {
                const name = image.substring(6, image.length);
                this._ios.image = UIImage.imageNamed(name);
            }
            else {
                this._ios.image = UIImage.imageNamed(image);
            }
        }
    }
    get ios() {
        return this._ios;
    }
}
var CompletionModeImpl = /** @class */ (function (_super) {
    __extends(CompletionModeImpl, _super);
    function CompletionModeImpl() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    CompletionModeImpl.new = function () {
        return _super.new.call(this);
    };
    CompletionModeImpl.StartsWith = function (input, suggestions, owner) {
        var result = NSMutableArray.alloc().initWithCapacity(suggestions.length);
        var nsResult = new Array();
        for (var i = 0; i < suggestions.length; i++) {
            var current = suggestions.getItem(i);
            var upperCase = current.ios.text.toUpperCase();
            if (upperCase['startsWith'](input.toUpperCase())) {
                result.addObject(current.ios);
                nsResult.push(current);
            }
        }
        owner._filteredItems = nsResult;
        return result;
    };
    CompletionModeImpl.Contains = function (input, suggestions, owner) {
        var result = NSMutableArray.alloc().initWithCapacity(suggestions.length);
        var nsResult = new Array();
        for (var i = 0; i < suggestions.length; i++) {
            var current = suggestions.getItem(i);
            var upperCase = current.ios.text.toUpperCase();
            if (upperCase.indexOf(input.toUpperCase()) !== -1) {
                result.addObject(current.ios);
                nsResult.push(current);
            }
        }
        owner._filteredItems = nsResult;
        return result;
    };
    return CompletionModeImpl;
}(NSObject));
var SuggestionViewDataSourceImpl = /** @class */ (function (_super) {
    __extends(SuggestionViewDataSourceImpl, _super);
    function SuggestionViewDataSourceImpl() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    SuggestionViewDataSourceImpl.new = function () {
        return _super.new.call(this);
    };
    SuggestionViewDataSourceImpl.prototype.initWithOwner = function (owner) {
        this._owner = new WeakRef(owner);
        return this;
    };
    SuggestionViewDataSourceImpl.prototype.collectionViewNumberOfItemsInSection = function (collectionView, section) {
        var owner = this._owner.get();
        // todo: update to support custom DataSource object from owner
        return owner.filteredItems ? owner.filteredItems.length : 0;
    };
    SuggestionViewDataSourceImpl.prototype.collectionViewCellForItemAtIndexPath = function (collectionView, indexPath) {
        var owner = this._owner.get();
        var cell = collectionView.dequeueReusableCellWithReuseIdentifierForIndexPath("defaultCell", indexPath);
        if (!cell.owner) {
            cell.backgroundView.stroke = null;
            cell.owner = owner;
            var template = new Object();
            template.itemView = owner.getViewForViewType(commonModule.AutoCompleteViewType.ItemView);
            cell.view = template;
            commonModule.setupCssScope(owner, template.itemView);
            owner.suggestionView._addView(template.itemView);
            cell.contentView.addSubview(template.itemView.ios);
        }
        var model = owner._filteredItems[indexPath.row];
        cell.view.itemView.bindingContext = model;
        var args = {
            eventName: commonModule.RadAutoCompleteTextView.itemLoadingEvent,
            index: indexPath.row,
            view: cell.view.itemView,
            ios: cell,
            data: model
        };
        owner.notify(args);
        return cell;
    };
    SuggestionViewDataSourceImpl.prototype.numberOfSectionsInCollectionView = function (collectionView) {
        return 1;
    };
    SuggestionViewDataSourceImpl.ObjCProtocols = [UICollectionViewDataSource, UICollectionViewDelegate];
    return SuggestionViewDataSourceImpl;
}(NSObject));
var AutoCompleteAsyncDataSourceImpl = /** @class */ (function (_super) {
    __extends(AutoCompleteAsyncDataSourceImpl, _super);
    function AutoCompleteAsyncDataSourceImpl() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.currentCompletionMode = CompletionModeImpl.StartsWith;
        return _this;
    }
    AutoCompleteAsyncDataSourceImpl.new = function () {
        return _super.new.call(this);
    };
    AutoCompleteAsyncDataSourceImpl.prototype.initWithOwner = function (owner) {
        this._owner = new WeakRef(owner);
        return this;
    };
    AutoCompleteAsyncDataSourceImpl.prototype.autoCompleteCompletionsForString = function (autocomplete, input) {
        var self = this;
        var owner = self._owner.get();
        owner.asyncCall(input).then(function (items) {
            var radAutoComplete = self._owner.get();
            var result = NSMutableArray.new();
            var nsResult = new Array();
            radAutoComplete.items = new ObservableArray();
            if (radAutoComplete.completionMode === commonModule.AutoCompleteCompletionMode.StartsWith) {
                for (var i = 0; i < items.length; i++) {
                    var current = items[i];
                    var upperCase = current.ios.text.toUpperCase();
                    if (upperCase.startsWith(input.toUpperCase())) {
                        result.addObject(current.ios);
                        nsResult.push(current);
                        radAutoComplete.items.push(current);
                    }
                }
            }
            else {
                for (var i = 0; i < items.length; i++) {
                    var current = items[i];
                    var upperCase = current.ios.text.toUpperCase();
                    if (upperCase.indexOf(input.toUpperCase()) !== -1) {
                        result.addObject(current.ios);
                        nsResult.push(current);
                        radAutoComplete.items.push(current);
                    }
                }
            }
            radAutoComplete._filteredItems = nsResult;
            radAutoComplete._ios.completeSuggestionViewPopulation(result);
        });
    };
    AutoCompleteAsyncDataSourceImpl.ObjCProtocols = [TKAutoCompleteDataSource];
    return AutoCompleteAsyncDataSourceImpl;
}(NSObject));
var AutoCompleteDataSourceImpl = /** @class */ (function (_super) {
    __extends(AutoCompleteDataSourceImpl, _super);
    function AutoCompleteDataSourceImpl() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.currentCompletionMode = CompletionModeImpl.StartsWith;
        return _this;
    }
    AutoCompleteDataSourceImpl.new = function () {
        return _super.new.call(this);
    };
    AutoCompleteDataSourceImpl.prototype.initWithOwner = function (owner) {
        this._owner = new WeakRef(owner);
        return this;
    };
    AutoCompleteDataSourceImpl.prototype.autoCompleteCompletionForPrefix = function (autocomplete, prefix) {
        var suggestions = NSMutableArray.new();
        var owner = this._owner.get();
        if (prefix === "") {
            owner.nativeView.suggestionView.hide();
            return suggestions;
        }
        else {
            return this.currentCompletionMode(prefix, owner.items, owner);
        }
    };
    AutoCompleteDataSourceImpl.ObjCProtocols = [TKAutoCompleteDataSource];
    return AutoCompleteDataSourceImpl;
}(NSObject));
var AutoCompleteDelegateImpl = /** @class */ (function (_super) {
    __extends(AutoCompleteDelegateImpl, _super);
    function AutoCompleteDelegateImpl() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    AutoCompleteDelegateImpl.new = function () {
        return _super.new.call(this);
    };
    AutoCompleteDelegateImpl.prototype.initWithOwner = function (owner) {
        this._owner = new WeakRef(owner);
        this._firstInput = true;
        return this;
    };
    AutoCompleteDelegateImpl.prototype.autoCompleteWillShowSuggestionList = function (autocomplete, suggestionList) {
        var owner = this._owner.get();
        var args = new commonModule.AutoCompleteEventData(owner, commonModule.RadAutoCompleteTextView.suggestionViewBecameVisibleEvent, undefined, undefined);
        owner.notify(args);
    };
    AutoCompleteDelegateImpl.prototype.autoCompleteDidChangeText = function (autocomplete, text) {
        var owner = this._owner.get();
        if (owner.text !== text) {
            var args = new commonModule.AutoCompleteEventData(owner, commonModule.RadAutoCompleteTextView.textChangedEvent, text, undefined);
            owner.text = text;
            owner.notify(args);
        }
    };
    AutoCompleteDelegateImpl.prototype.autoCompleteDidChangeFocus = function (autocomplete, hasFocus) {
        var owner = this._owner.get();
        if (hasFocus) {
            owner.notify({ eventName: EditableTextBase.focusEvent, object: owner });
        }
        else {
            owner.notify({ eventName: EditableTextBase.blurEvent, object: owner });
        }
    };
    AutoCompleteDelegateImpl.prototype.autoCompleteDidLayoutSubviews = function (autocomplete) {
        var owner = this._owner.get();
        var position = getPositionFromFrame(autocomplete.closeButton.frame);
        owner.clearButton.layout(position.left, position.top, position.right, position.bottom);
    };
    AutoCompleteDelegateImpl.prototype.autoCompleteDidLayoutSuggestionView = function (autocomplete) {
        var owner = this._owner.get();
        var position = getPositionFromFrame(autocomplete.suggestionView.frame);
        owner.suggestionView.layout(position.left, position.top, position.right, position.bottom);
    };
    AutoCompleteDelegateImpl.prototype.autoCompleteDidLayoutTokenView = function (autocomplete, tokenView) {
        var owner = this._owner.get();
        var tokenPosition = getPositionFromFrame(tokenView.frame);
        var tokenClearPosition = getPositionFromFrame(tokenView.removeButton.frame);
        for (var i = 0; i < owner._tokens.length; i++) {
            var currentToken = owner._tokens[i];
            if (currentToken.ios === tokenView) {
                currentToken.layout(tokenPosition.left, tokenPosition.top, tokenPosition.right, tokenPosition.bottom);
                currentToken.clearButton.layout(tokenClearPosition.left, tokenClearPosition.top, tokenClearPosition.right, tokenClearPosition.bottom);
            }
        }
    };
    AutoCompleteDelegateImpl.prototype.autoCompleteDidSelectTokenView = function (autocomplete, tokenView) {
        var owner = this._owner.get();
        for (var i = 0; i < owner._tokens.length; i++) {
            var currentToken = owner._tokens[i];
            if (currentToken.ios === tokenView) {
                currentToken._goToVisualState("selected");
            }
        }
    };
    AutoCompleteDelegateImpl.prototype.autoCompleteDidDeselectTokenView = function (autocomplete, tokenView) {
        var owner = this._owner.get();
        for (var i = 0; i < owner._tokens.length; i++) {
            var currentToken = owner._tokens[i];
            if (currentToken.ios === tokenView) {
                currentToken._goToVisualState("normal");
            }
        }
    };
    AutoCompleteDelegateImpl.prototype.autoCompleteDidAddTokenAtIndex = function (autocomplete, token, index) {
        var owner = this._owner.get();
        var tokenModel = nativeModelToNSModel(token, owner.filteredItems);
        var nativeTokenView = owner.ios.tokenViewAtIndex(index);
        var tokenView = new Token(nativeTokenView);
        owner._tokens.push(tokenView);
        commonModule.setupCssScope(owner, tokenView);
        commonModule.setupCssScope(owner, tokenView.clearButton);
        owner._addView(tokenView);
        owner._onCssStateChange();
        owner._selectedTokens.push(tokenModel);
        var args = new commonModule.AutoCompleteEventData(owner, commonModule.RadAutoCompleteTextView.tokenAddedEvent, token.text, tokenModel);
        owner.notify(args);
        owner.requestLayout();
    };
    AutoCompleteDelegateImpl.prototype.autoCompleteDidRemoveToken = function (autocomplete, token) {
        var owner = this._owner.get();
        var tokenModel = nativeModelToNSModel(token, owner._selectedTokens);
        var indexToRemove = owner._selectedTokens.indexOf(tokenModel);
        if (indexToRemove >= 0) {
            owner._selectedTokens.splice(indexToRemove, 1);
            owner._tokens.splice(indexToRemove, 1);
        }
        var args = new commonModule.AutoCompleteEventData(owner, commonModule.RadAutoCompleteTextView.tokenRemovedEvent, token.text, tokenModel);
        owner.notify(args);
        // TODO: Investigate this in native iOS why it requires an time out.
        setTimeout(function () {
            owner.requestLayout();
        }, 10);
    };
    AutoCompleteDelegateImpl.prototype.autoCompleteDidSelectToken = function (autocomplete, token) {
        var owner = this._owner.get();
        var tokenModel = nativeModelToNSModel(token, owner._selectedTokens);
        var args = new commonModule.AutoCompleteEventData(owner, commonModule.RadAutoCompleteTextView.tokenSelectedEvent, token.text, tokenModel);
        owner.notify(args);
    };
    AutoCompleteDelegateImpl.prototype.autoCompleteDidDeselectToken = function (autocomplete, token) {
        var owner = this._owner.get();
        var tokenModel = nativeModelToNSModel(token, owner._selectedTokens);
        var args = new commonModule.AutoCompleteEventData(owner, commonModule.RadAutoCompleteTextView.tokenDeselectedEvent, token.text, tokenModel);
        owner.notify(args);
    };
    AutoCompleteDelegateImpl.prototype.autoCompleteDidAutoComplete = function (autocomplete, token) {
        var owner = this._owner.get();
        var tokenModel = nativeModelToNSModel(token, owner.filteredItems);
        var args = new commonModule.AutoCompleteEventData(owner, commonModule.RadAutoCompleteTextView.didAutoCompleteEvent, token.text, tokenModel);
        owner.notify(args);
    };
    AutoCompleteDelegateImpl.ObjCProtocols = [TKAutoCompleteDelegate];
    return AutoCompleteDelegateImpl;
}(NSObject));
function nativeModelToNSModel(nativeItem, items) {
    if (!items) {
        return null;
    }
    for (let i = 0; i < items.length; i++) {
        const tokenModel = items[i];
        if (+tokenModel.ios.tokenId === +nativeItem.tokenId) {
            return tokenModel;
        }
    }
    return null;
}
export class RadAutoCompleteTextView extends commonModule.RadAutoCompleteTextView {
    constructor() {
        super(...arguments);
        this._selectedTokens = new Array();
    }
    createNativeView() {
        this._ios = TKAutoCompleteTextView.new();
        this._ios.minimumCharactersToSearch = 1;
        if (!this._dataSource) {
            this._dataSource = AutoCompleteDataSourceImpl.new().initWithOwner(this);
            this._dataSource.currentCompletionMode = CompletionModeImpl.StartsWith;
            this._ios.dataSource = this._dataSource;
            this.adjustCompletionMode(this.completionMode);
        }
        this._ios.suggestionView.registerClassForCellWithReuseIdentifier(SuggestionViewCell.class(), "defaultCell");
        this._suggestionViewDataSource = SuggestionViewDataSourceImpl.new().initWithOwner(this);
        this._ios.suggestionView.dataSource = this._suggestionViewDataSource;
        this._delegate = AutoCompleteDelegateImpl.new().initWithOwner(this);
        this._ios.delegate = this._delegate;
        this._ngKey = Object.keys(this).find(key => key.startsWith('_ngcontent'));
        this._vueKey = Object.keys(this).find(key => key.startsWith('data-v'));
        this._clearButton = new ClearButton(this._ios);
        commonModule.setupCssScope(this, this._clearButton);
        this._addView(this._clearButton);
        this._noResultsLabel = new NoResultsLabel(this._ios.noResultsLabel);
        commonModule.setupCssScope(this, this._noResultsLabel);
        this._addView(this._noResultsLabel);
        if (!this.suggestionView) {
            this.suggestionView = new SuggestionView();
        }
        return this._ios;
    }
    disposeNativeView() {
        this._ios.delegate = undefined;
        this._ios.dataSource = undefined;
        this._dataSource = undefined;
        this._ios = undefined;
        this._delegate = undefined;
    }
    resetAutoComplete() {
        this._ios.resetAutocompleteState();
        this.requestLayout();
    }
    get clearButton() {
        return this._clearButton;
    }
    get noResultsLabel() {
        return this._noResultsLabel;
    }
    get filteredItems() {
        if (this._filteredItems === undefined) {
            this._filteredItems = new Array();
        }
        return this._filteredItems;
    }
    get selectedTokens() {
        return this._selectedTokens;
    }
    addToken(token) {
        if (!token) {
            return;
        }
        this.filteredItems.push(token);
        this._ios.addToken(token.ios);
        this.requestLayout();
    }
    insertTokenAtIndex(token, index) {
        if (!token) {
            return;
        }
        this.filteredItems.push(token);
        this._ios.insertTokenAtIndex(token.ios, index);
    }
    removeToken(token) {
        if (!token) {
            return;
        }
        this._ios.removeToken(token.ios);
        this.requestLayout();
    }
    removeTokenAtIndex(index) {
        this._ios.removeTokenAtIndex(index);
        this.requestLayout();
    }
    removeAllTokens() {
        this._ios.removeAllTokens();
        this.requestLayout();
    }
    tokens() {
        return this._ios.tokens;
    }
    tokenAtIndex(index) {
        return this._ios.tokenAtIndex(index);
    }
    findSuggestions() {
        return this._ios.findSuggestions();
    }
    [commonModule.RadAutoCompleteTextView.displayModeProperty.getDefault]() {
        return commonModule.RadAutoCompleteTextView.displayModeProperty.defaultValue;
    }
    [commonModule.RadAutoCompleteTextView.displayModeProperty.setNative](newValue) {
        this.adjustDisplayMode(newValue);
    }
    [commonModule.RadAutoCompleteTextView.layoutModeProperty.getDefault]() {
        return commonModule.RadAutoCompleteTextView.layoutModeProperty.defaultValue;
    }
    [commonModule.RadAutoCompleteTextView.layoutModeProperty.setNative](newValue) {
        this.adjustLayoutMode(newValue);
    }
    [commonModule.RadAutoCompleteTextView.suggestModeProperty.getDefault]() {
        return commonModule.RadAutoCompleteTextView.suggestModeProperty.defaultValue;
    }
    [commonModule.RadAutoCompleteTextView.suggestModeProperty.setNative](newValue) {
        this.adjustSuggestMode(newValue);
    }
    [commonModule.RadAutoCompleteTextView.completionModeProperty.getDefault]() {
        return commonModule.RadAutoCompleteTextView.completionModeProperty.defaultValue;
    }
    [commonModule.RadAutoCompleteTextView.completionModeProperty.setNative](newValue) {
        this.adjustCompletionMode(newValue);
    }
    [commonModule.RadAutoCompleteTextView.loadSuggestionsAsyncProperty.setNative](newValue) {
        this.asyncCall = newValue;
        this._dataSource = AutoCompleteAsyncDataSourceImpl.new().initWithOwner(this);
        this._dataSource.currentCompletionMode = CompletionModeImpl.StartsWith;
        this._ios.dataSource = this._dataSource;
    }
    [commonModule.RadAutoCompleteTextView.suggestionViewProperty.setNative](newValue) {
        const suggestionView = newValue;
        suggestionView.owner = this;
        commonModule.setupCssScope(this, suggestionView);
        this._addView(suggestionView);
        suggestionView.ios = this._ios.suggestionView;
        if (suggestionView.suggestionViewHeight) {
            this._ios.suggestionViewHeight = suggestionView.suggestionViewHeight;
        }
        this._ios.suggestionView.reloadData();
        suggestionView.updateView();
    }
    [commonModule.RadAutoCompleteTextView.minimumCharactersToSearchProperty.getDefault]() {
        return this._ios.minimumCharactersToSearch;
    }
    [commonModule.RadAutoCompleteTextView.minimumCharactersToSearchProperty.setNative](newValue) {
        this._ios.minimumCharactersToSearch = newValue;
    }
    [commonModule.RadAutoCompleteTextView.tokenItemSpacingProperty.getDefault]() {
        return this._ios.tokenInset;
    }
    [commonModule.RadAutoCompleteTextView.tokenItemSpacingProperty.setNative](newValue) {
        this._ios.tokenInset = newValue;
    }
    [commonModule.RadAutoCompleteTextView.noResultsTextProperty.getDefault]() {
        return this._ios.noResultsText;
    }
    [commonModule.RadAutoCompleteTextView.noResultsTextProperty.setNative](newValue) {
        this._ios.noResultsText = newValue;
    }
    [commonModule.RadAutoCompleteTextView.showCloseButtonProperty.getDefault]() {
        return this._ios.showCloseButton;
    }
    [commonModule.RadAutoCompleteTextView.showCloseButtonProperty.setNative](newValue) {
        this._ios.showCloseButton = newValue;
    }
    [commonModule.RadAutoCompleteTextView.hintProperty.getDefault]() {
        return this._ios.textField.placeholder;
    }
    [commonModule.RadAutoCompleteTextView.hintProperty.setNative](newValue) {
        this._ios.textField.placeholder = newValue;
    }
    [commonModule.RadAutoCompleteTextView.textProperty.getDefault]() {
        return this._ios.textField.text;
    }
    [commonModule.RadAutoCompleteTextView.textProperty.setNative](newValue) {
        if (this._ios.textField.text === newValue) {
            return;
        }
        this._ios.textField.text = newValue;
        this._ios.updateCloseButtonVisibility();
        const args = new commonModule.AutoCompleteEventData(this, commonModule.RadAutoCompleteTextView.textChangedEvent, newValue, undefined);
        this.notify(args);
    }
    [commonModule.RadAutoCompleteTextView.closeButtonImageSrcProperty.getDefault]() {
        return null;
    }
    [commonModule.RadAutoCompleteTextView.closeButtonImageSrcProperty.setNative](newValue) {
        const imageSrc = this.closeButtonImageSrc;
        let image;
        if (!imageSrc) {
            image = UIImage.new();
        }
        else if (imageSrc.startsWith("res://")) {
            const name = imageSrc.substring(6, imageSrc.length);
            image = UIImage.imageNamed(name);
        }
        else {
            image = UIImage.imageNamed(imageSrc);
        }
        this._ios.closeButton.setImageForState(image, 0 /* Normal */);
    }
    [colorProperty.getDefault]() {
        return null;
    }
    [colorProperty.setNative](value) {
        const nativeColor = value instanceof Color ? value.ios : value;
        if (!nativeColor) {
            this._ios.textField.textColor = UIColor.blackColor;
            this._ios.appendLabel.textColor = UIColor.grayColor;
        }
        else {
            this._ios.textField.textColor = nativeColor;
            this._ios.appendLabel.textColor = nativeColor.colorWithAlphaComponent(0.5);
        }
    }
    [fontInternalProperty.getDefault]() {
        return this._ios.textField.font;
    }
    [fontInternalProperty.setNative](value) {
        const nativeFont = value instanceof Font ? value.getUIFont(this._ios.textField.font) : value;
        this._ios.textField.font = nativeFont;
        this._ios.appendLabel.font = nativeFont;
    }
    [paddingLeftProperty.getDefault]() {
        return this._ios.insets.left;
    }
    [paddingLeftProperty.setNative](value) {
        let borderLeft = isNaN(+this.style.borderLeftWidth) ? 0 : +this.style.borderLeftWidth;
        let currentInsets = this._ios.insets;
        let insets = new UIEdgeInsets({
            left: value + borderLeft,
            top: currentInsets.top,
            right: currentInsets.right,
            bottom: currentInsets.bottom
        });
        this._ios.insets = insets;
    }
    [borderLeftWidthProperty.getDefault]() {
        return 0;
    }
    [borderLeftWidthProperty.setNative](value) {
        let paddingLeft = isNaN(+this.style.paddingLeft) ? 0 : +this.style.paddingLeft;
        let currentInsets = this._ios.insets;
        let insets = new UIEdgeInsets({
            left: value + paddingLeft,
            top: currentInsets.top,
            right: currentInsets.right,
            bottom: currentInsets.bottom
        });
        this._ios.insets = insets;
    }
    [paddingTopProperty.getDefault]() {
        return this._ios.insets.top;
    }
    [paddingTopProperty.setNative](value) {
        let borderTop = isNaN(+this.style.borderTopWidth) ? 0 : +this.style.borderTopWidth;
        let currentInsets = this._ios.insets;
        let insets = new UIEdgeInsets({
            left: currentInsets.left,
            top: value + borderTop,
            right: currentInsets.right,
            bottom: currentInsets.bottom
        });
        this._ios.insets = insets;
    }
    [borderTopWidthProperty.getDefault]() {
        return 0;
    }
    [borderTopWidthProperty.setNative](value) {
        let paddingTop = isNaN(+this.style.paddingTop) ? 0 : +this.style.paddingTop;
        let currentInsets = this._ios.insets;
        let insets = new UIEdgeInsets({
            left: currentInsets.left,
            top: value + paddingTop,
            right: currentInsets.right,
            bottom: currentInsets.bottom
        });
        this._ios.insets = insets;
    }
    [paddingRightProperty.getDefault]() {
        return this._ios.insets.right;
    }
    [paddingRightProperty.setNative](value) {
        let borderRight = isNaN(+this.style.borderRightWidth) ? 0 : +this.style.borderRightWidth;
        let currentInsets = this._ios.insets;
        let insets = new UIEdgeInsets({
            left: currentInsets.left,
            top: currentInsets.top,
            right: value + borderRight,
            bottom: currentInsets.bottom
        });
        this._ios.insets = insets;
    }
    [borderRightWidthProperty.getDefault]() {
        return 0;
    }
    [borderRightWidthProperty.setNative](value) {
        let paddingRight = isNaN(+this.style.paddingRight) ? 0 : +this.style.paddingRight;
        let currentInsets = this._ios.insets;
        let insets = new UIEdgeInsets({
            left: currentInsets.left,
            top: currentInsets.top,
            right: value + paddingRight,
            bottom: currentInsets.bottom
        });
        this._ios.insets = insets;
    }
    [paddingBottomProperty.getDefault]() {
        return this._ios.insets.bottom;
    }
    [paddingBottomProperty.setNative](value) {
        let borderBottom = isNaN(+this.style.borderBottomWidth) ? 0 : +this.style.borderBottomWidth;
        let currentInsets = this._ios.insets;
        let insets = new UIEdgeInsets({
            left: currentInsets.left,
            top: currentInsets.top,
            right: currentInsets.right,
            bottom: value + borderBottom,
        });
        this._ios.insets = insets;
    }
    [borderBottomWidthProperty.getDefault]() {
        return 0;
    }
    [borderBottomWidthProperty.setNative](value) {
        let paddingBottom = isNaN(+this.style.paddingBottom) ? 0 : +this.style.paddingBottom;
        let currentInsets = this._ios.insets;
        let insets = new UIEdgeInsets({
            left: currentInsets.left,
            top: currentInsets.top,
            right: currentInsets.right,
            bottom: value + paddingBottom
        });
        this._ios.insets = insets;
    }
    tokenModelWithText(text) {
        if (this.items) {
            for (let i = 0; i < this.items.length; i++) {
                const current = this.items.getItem(i);
                if (current.text === text) {
                    return current;
                }
            }
        }
        return null;
    }
    [commonModule.RadAutoCompleteTextView.readOnlyProperty.getDefault]() {
        return this._ios.readOnly;
    }
    [commonModule.RadAutoCompleteTextView.readOnlyProperty.setNative](newValue) {
        this._ios.readOnly = newValue;
    }
    adjustCompletionMode(value) {
        if (this._ios && value && this._dataSource) {
            if (value === commonModule.AutoCompleteCompletionMode.StartsWith) {
                this._dataSource.currentCompletionMode = CompletionModeImpl.StartsWith;
            }
            else {
                this._dataSource.currentCompletionMode = CompletionModeImpl.Contains;
            }
        }
    }
    adjustDisplayMode(value) {
        if (this._ios && value) {
            this._ios.displayMode = (value === commonModule.AutoCompleteDisplayMode.Plain) ?
                0 /* Plain */ :
                1 /* Tokens */;
        }
    }
    adjustSuggestMode(value) {
        if (this._ios && value) {
            if (value === commonModule.AutoCompleteSuggestMode.Suggest) {
                this._ios.suggestMode = 0 /* Suggest */;
            }
            else if (value === commonModule.AutoCompleteSuggestMode.Append) {
                this._ios.suggestMode = 1 /* Append */;
            }
            else {
                this._ios.suggestMode = 2 /* SuggestAppend */;
            }
        }
    }
    adjustLayoutMode(value) {
        if (value && this._ios) {
            this._ios.layoutMode = (value === commonModule.AutoCompleteLayoutMode.Horizontal) ?
                0 /* Horizontal */ :
                1 /* Wrap */;
            this.requestLayout();
        }
    }
}
export class Token extends commonModule.Token {
    constructor(nativeView) {
        super();
        this._ios = nativeView;
        this._clearButton = new TokenClearButton(this._ios);
        this._addView(this._clearButton);
    }
    _updateAutoCompleteTokenSelectedHandlers(subscribe) {
        this._ios.useHighlighting = !subscribe;
    }
    createNativeView() {
        return this._ios;
    }
    disposeNativeView() {
        this._ios = null;
    }
    get clearButton() {
        return this._clearButton;
    }
    eachChild(callback) {
        if (this.clearButton) {
            callback(this.clearButton);
        }
    }
    [colorProperty.getDefault]() {
        return this._ios.textLabel.textColor;
    }
    [colorProperty.setNative](value) {
        const nativeColor = value instanceof Color ? value.ios : value;
        this._ios.textLabel.textColor = nativeColor;
    }
    [fontInternalProperty.getDefault]() {
        return this._ios.textLabel.font;
    }
    [fontInternalProperty.setNative](value) {
        const nativeFont = value instanceof Font ? value.getUIFont(this._ios.textLabel.font) : value;
        this._ios.textLabel.font = nativeFont;
    }
    [paddingLeftProperty.getDefault]() {
        return this._ios.insets.left;
    }
    [paddingLeftProperty.setNative](value) {
        let borderLeft = isNaN(+this.style.borderLeftWidth) ? 0 : +this.style.borderLeftWidth;
        let currentInsets = this._ios.insets;
        let insets = new UIEdgeInsets({
            left: value + borderLeft,
            top: currentInsets.top,
            right: currentInsets.right,
            bottom: currentInsets.bottom
        });
        this._ios.insets = insets;
    }
    [borderLeftWidthProperty.getDefault]() {
        return 0;
    }
    [borderLeftWidthProperty.setNative](value) {
        let paddingLeft = isNaN(+this.style.paddingLeft) ? 0 : +this.style.paddingLeft;
        let currentInsets = this._ios.insets;
        let insets = new UIEdgeInsets({
            left: value + paddingLeft,
            top: currentInsets.top,
            right: currentInsets.right,
            bottom: currentInsets.bottom
        });
        this._ios.insets = insets;
    }
    [paddingTopProperty.getDefault]() {
        return this._ios.insets.top;
    }
    [paddingTopProperty.setNative](value) {
        let borderTop = isNaN(+this.style.borderTopWidth) ? 0 : +this.style.borderTopWidth;
        let currentInsets = this._ios.insets;
        let insets = new UIEdgeInsets({
            left: currentInsets.left,
            top: value + borderTop,
            right: currentInsets.right,
            bottom: currentInsets.bottom
        });
        this._ios.insets = insets;
    }
    [borderTopWidthProperty.getDefault]() {
        return 0;
    }
    [borderTopWidthProperty.setNative](value) {
        let paddingTop = isNaN(+this.style.paddingTop) ? 0 : +this.style.paddingTop;
        let currentInsets = this._ios.insets;
        let insets = new UIEdgeInsets({
            left: currentInsets.left,
            top: value + paddingTop,
            right: currentInsets.right,
            bottom: currentInsets.bottom
        });
        this._ios.insets = insets;
    }
    [paddingRightProperty.getDefault]() {
        return this._ios.insets.right;
    }
    [paddingRightProperty.setNative](value) {
        let borderRight = isNaN(+this.style.borderRightWidth) ? 0 : +this.style.borderRightWidth;
        let currentInsets = this._ios.insets;
        let insets = new UIEdgeInsets({
            left: currentInsets.left,
            top: currentInsets.top,
            right: value + borderRight,
            bottom: currentInsets.bottom
        });
        this._ios.insets = insets;
    }
    [borderRightWidthProperty.getDefault]() {
        return 0;
    }
    [borderRightWidthProperty.setNative](value) {
        let paddingRight = isNaN(+this.style.paddingRight) ? 0 : +this.style.paddingRight;
        let currentInsets = this._ios.insets;
        let insets = new UIEdgeInsets({
            left: currentInsets.left,
            top: currentInsets.top,
            right: value + paddingRight,
            bottom: currentInsets.bottom
        });
        this._ios.insets = insets;
    }
    [paddingBottomProperty.getDefault]() {
        return this._ios.insets.bottom;
    }
    [paddingBottomProperty.setNative](value) {
        let borderBottom = isNaN(+this.style.borderBottomWidth) ? 0 : +this.style.borderBottomWidth;
        let currentInsets = this._ios.insets;
        let insets = new UIEdgeInsets({
            left: currentInsets.left,
            top: currentInsets.top,
            right: currentInsets.right,
            bottom: value + borderBottom,
        });
        this._ios.insets = insets;
    }
    [borderBottomWidthProperty.getDefault]() {
        return 0;
    }
    [borderBottomWidthProperty.setNative](value) {
        let paddingBottom = isNaN(+this.style.paddingBottom) ? 0 : +this.style.paddingBottom;
        let currentInsets = this._ios.insets;
        let insets = new UIEdgeInsets({
            left: currentInsets.left,
            top: currentInsets.top,
            right: currentInsets.right,
            bottom: value + paddingBottom
        });
        this._ios.insets = insets;
    }
}
__decorate([
    PseudoClassHandler("selected", "normal"),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Boolean]),
    __metadata("design:returntype", void 0)
], Token.prototype, "_updateAutoCompleteTokenSelectedHandlers", null);
export class NoResultsLabel extends commonModule.NoResultsLabel {
    constructor(nativeView) {
        super();
        this._ios = nativeView;
    }
    createNativeView() {
        return this._ios;
    }
    disposeNativeView() {
        this._ios = null;
    }
    [colorProperty.getDefault]() {
        return null;
    }
    [colorProperty.setNative](value) {
        const nativeColor = value instanceof Color ? value.ios : value;
        if (nativeColor) {
            this._ios.textColor = nativeColor;
        }
        else {
            this._ios.textColor = UIColor.blackColor;
        }
    }
    [fontInternalProperty.getDefault]() {
        return this._ios.font;
    }
    [fontInternalProperty.setNative](value) {
        const nativeFont = value instanceof Font ? value.getUIFont(this._ios.font) : value;
        this._ios.font = nativeFont;
    }
}
export class ClearButton extends commonModule.ClearButton {
    constructor(owner) {
        super();
        this._owner = owner;
        this._ios = owner.closeButton;
    }
    createNativeView() {
        return this._ios;
    }
    disposeNativeView() {
        this._ios = null;
    }
    [colorProperty.getDefault]() {
        return null;
    }
    [colorProperty.setNative](value) {
        const nativeColor = value instanceof Color ? value.ios : value;
        this._ios.circleBackgroundColor = nativeColor;
        this._ios.tintColor = nativeColor;
    }
    [widthProperty.getDefault]() {
        return this._owner.closeButtonWidth;
    }
    [widthProperty.setNative](value) {
        this._owner.closeButtonWidth = value;
    }
    [heightProperty.getDefault]() {
        return this._owner.closeButtonWidth;
    }
    [heightProperty.setNative](value) {
        this._owner.closeButtonHeight = value;
    }
    [backgroundImageProperty.getDefault]() {
        return null;
    }
    [backgroundImageProperty.setNative](value) {
        this._ios.drawShape = (value === null);
    }
}
export class TokenClearButton extends commonModule.TokenClearButton {
    constructor(token) {
        super();
        this._token = token;
        this._ios = token.removeButton;
    }
    createNativeView() {
        return this._ios;
    }
    disposeNativeView() {
        this._ios = null;
    }
    [colorProperty.getDefault]() {
        return this._ios.tintColor;
    }
    [colorProperty.setNative](value) {
        const nativeColor = value instanceof Color ? value.ios : value;
        this._ios.tintColor = nativeColor;
        this._ios.setNeedsDisplay();
    }
    [backgroundImageProperty.getDefault]() {
        return null;
    }
    [backgroundImageProperty.setNative](value) {
        this._ios.drawShape = (value === null);
        this._ios.setNeedsDisplay();
    }
    [widthProperty.getDefault]() {
        return this._token.removeButtonWidth;
    }
    [widthProperty.setNative](value) {
        this._token.removeButtonWidth = value;
    }
    [heightProperty.getDefault]() {
        return this._token.removeButtonHeight;
    }
    [heightProperty.setNative](value) {
        this._token.removeButtonHeight = value;
    }
}
function getPositionFromFrame(frame) {
    const left = Utils.layout.round(Utils.layout.toDevicePixels(frame.origin.x));
    const top = Utils.layout.round(Utils.layout.toDevicePixels(frame.origin.y));
    const right = Utils.layout.round(Utils.layout.toDevicePixels(frame.origin.x + frame.size.width));
    const bottom = Utils.layout.round(Utils.layout.toDevicePixels(frame.origin.y + frame.size.height));
    return { left, right, top, bottom };
}
//# sourceMappingURL=index.ios.js.map