Object.defineProperty(exports, "__esModule", { value: true });
exports.TokenClearButton = exports.ClearButton = exports.NoResultsLabel = exports.Token = exports.RadAutoCompleteTextView = exports.TokenModel = exports.SuggestionView = exports.knownTemplates = void 0;
var core_1 = require("@nativescript/core");
var commonModule = require("./ui-autocomplete.common");
__exportStar(require("./ui-autocomplete.common"), exports);
var knownTemplates;
(function (knownTemplates) {
    knownTemplates.suggestionItemTemplate = "suggestionItemTemplate";
})(knownTemplates = exports.knownTemplates || (exports.knownTemplates = {}));
var SuggestionView = /** @class */ (function (_super) {
    __extends(SuggestionView, _super);
    function SuggestionView() {
        return _super.call(this) || this;
    }
    Object.defineProperty(SuggestionView.prototype, "ios", {
        get: function () {
            return this._ios;
        },
        set: function (value) {
            this._ios = value;
            this.setNativeView(value);
        },
        enumerable: false,
        configurable: true
    });
    SuggestionView.prototype.createNativeView = function () {
        return this._ios ? this._ios : _super.prototype.createNativeView.call(this);
    };
    SuggestionView.prototype.updateView = function () {
        this.updateHeight();
    };
    SuggestionView.prototype[core_1.heightProperty.getDefault] = function () {
        return this.suggestionViewHeight;
    };
    SuggestionView.prototype[core_1.heightProperty.setNative] = function (value) {
        this.suggestionViewHeight = value;
    };
    SuggestionView.prototype.updateHeight = function () {
        if (this.owner && this.owner.nativeView && this.suggestionViewHeight) {
            this.owner.nativeView.suggestionViewHeight = this.suggestionViewHeight;
        }
    };
    SuggestionView.prototype.updateTemplate = function (value) {
        // TODO: Implement this, probably with Builder same as RadListView templates
        // this._ios.suggestionView = newValue;
        this.updateHeight();
    };
    SuggestionView.prototype.onSuggestionViewHeightChanged = function (oldValue, newValue) {
        this.updateHeight();
    };
    SuggestionView.prototype.onSuggestionItemTemplateChanged = function (oldValue, newValue) {
        this.updateTemplate(newValue);
    };
    return SuggestionView;
}(commonModule.SuggestionView));
exports.SuggestionView = SuggestionView;
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;
        core_1.View.layoutChild(this.owner, cellView, 0, 0, itemViewDimensions.measuredWidth, itemViewDimensions.measuredHeight);
        var measuredHeight = core_1.Utils.layout.round(core_1.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 = core_1.Utils.layout.makeMeasureSpec(0, core_1.Utils.layout.UNSPECIFIED);
            var widthSpec = core_1.Utils.layout.makeMeasureSpec(itemWidth, core_1.Utils.layout.EXACTLY);
            return core_1.View.measureChild(this.owner, cellView, widthSpec, heightSpec);
        }
        return undefined;
    };
    return SuggestionViewCell;
}(TKAutoCompleteSuggestionCell));
var TokenModel = /** @class */ (function (_super) {
    __extends(TokenModel, _super);
    function TokenModel(text, image) {
        var _this = _super.call(this, text, image) || this;
        var nativeText = NSString.stringWithCStringEncoding(text, NSUTF8StringEncoding);
        _this._ios = TKAutoCompleteToken.alloc().initWithText(nativeText);
        if (image) {
            if (image.startsWith("res://")) {
                var name_1 = image.substring(6, image.length);
                _this._ios.image = UIImage.imageNamed(name_1);
            }
            else {
                _this._ios.image = UIImage.imageNamed(image);
            }
        }
        return _this;
    }
    Object.defineProperty(TokenModel.prototype, "ios", {
        get: function () {
            return this._ios;
        },
        enumerable: false,
        configurable: true
    });
    return TokenModel;
}(commonModule.TokenModel));
exports.TokenModel = TokenModel;
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));
// suggestion view data source
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 core_1.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));
// AutoCompleteDataSource
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));
// AutoCompleteDelagate
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: core_1.EditableTextBase.focusEvent, object: owner });
        }
        else {
            owner.notify({ eventName: core_1.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 (var i = 0; i < items.length; i++) {
        var tokenModel = items[i];
        if (+tokenModel.ios.tokenId === +nativeItem.tokenId) {
            return tokenModel;
        }
    }
    return null;
}
var RadAutoCompleteTextView = /** @class */ (function (_super) {
    __extends(RadAutoCompleteTextView, _super);
    function RadAutoCompleteTextView() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this._selectedTokens = new Array();
        return _this;
    }
    RadAutoCompleteTextView.prototype.createNativeView = function () {
        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(function (key) { return key.startsWith('_ngcontent'); });
        this._vueKey = Object.keys(this).find(function (key) { return 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;
    };
    RadAutoCompleteTextView.prototype.disposeNativeView = function () {
        this._ios.delegate = undefined;
        this._ios.dataSource = undefined;
        this._dataSource = undefined;
        this._ios = undefined;
        this._delegate = undefined;
    };
    RadAutoCompleteTextView.prototype.resetAutoComplete = function () {
        this._ios.resetAutocompleteState();
        this.requestLayout();
    };
    Object.defineProperty(RadAutoCompleteTextView.prototype, "clearButton", {
        get: function () {
            return this._clearButton;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(RadAutoCompleteTextView.prototype, "noResultsLabel", {
        get: function () {
            return this._noResultsLabel;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(RadAutoCompleteTextView.prototype, "filteredItems", {
        get: function () {
            if (this._filteredItems === undefined) {
                this._filteredItems = new Array();
            }
            return this._filteredItems;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(RadAutoCompleteTextView.prototype, "selectedTokens", {
        get: function () {
            return this._selectedTokens;
        },
        enumerable: false,
        configurable: true
    });
    RadAutoCompleteTextView.prototype.addToken = function (token) {
        if (!token) {
            return;
        }
        this.filteredItems.push(token);
        this._ios.addToken(token.ios);
        this.requestLayout();
    };
    RadAutoCompleteTextView.prototype.insertTokenAtIndex = function (token, index) {
        if (!token) {
            return;
        }
        this.filteredItems.push(token);
        this._ios.insertTokenAtIndex(token.ios, index);
    };
    RadAutoCompleteTextView.prototype.removeToken = function (token) {
        if (!token) {
            return;
        }
        this._ios.removeToken(token.ios);
        this.requestLayout();
    };
    RadAutoCompleteTextView.prototype.removeTokenAtIndex = function (index) {
        this._ios.removeTokenAtIndex(index);
        this.requestLayout();
    };
    RadAutoCompleteTextView.prototype.removeAllTokens = function () {
        this._ios.removeAllTokens();
        this.requestLayout();
    };
    RadAutoCompleteTextView.prototype.tokens = function () {
        return this._ios.tokens;
    };
    RadAutoCompleteTextView.prototype.tokenAtIndex = function (index) {
        return this._ios.tokenAtIndex(index);
    };
    RadAutoCompleteTextView.prototype.findSuggestions = function () {
        return this._ios.findSuggestions();
    };
    RadAutoCompleteTextView.prototype[commonModule.RadAutoCompleteTextView.displayModeProperty.getDefault] = function () {
        return commonModule.RadAutoCompleteTextView.displayModeProperty.defaultValue;
    };
    RadAutoCompleteTextView.prototype[commonModule.RadAutoCompleteTextView.displayModeProperty.setNative] = function (newValue) {
        this.adjustDisplayMode(newValue);
    };
    RadAutoCompleteTextView.prototype[commonModule.RadAutoCompleteTextView.layoutModeProperty.getDefault] = function () {
        return commonModule.RadAutoCompleteTextView.layoutModeProperty.defaultValue;
    };
    RadAutoCompleteTextView.prototype[commonModule.RadAutoCompleteTextView.layoutModeProperty.setNative] = function (newValue) {
        this.adjustLayoutMode(newValue);
    };
    RadAutoCompleteTextView.prototype[commonModule.RadAutoCompleteTextView.suggestModeProperty.getDefault] = function () {
        return commonModule.RadAutoCompleteTextView.suggestModeProperty.defaultValue;
    };
    RadAutoCompleteTextView.prototype[commonModule.RadAutoCompleteTextView.suggestModeProperty.setNative] = function (newValue) {
        this.adjustSuggestMode(newValue);
    };
    RadAutoCompleteTextView.prototype[commonModule.RadAutoCompleteTextView.completionModeProperty.getDefault] = function () {
        return commonModule.RadAutoCompleteTextView.completionModeProperty.defaultValue;
    };
    RadAutoCompleteTextView.prototype[commonModule.RadAutoCompleteTextView.completionModeProperty.setNative] = function (newValue) {
        this.adjustCompletionMode(newValue);
    };
    RadAutoCompleteTextView.prototype[commonModule.RadAutoCompleteTextView.loadSuggestionsAsyncProperty.setNative] = function (newValue) {
        this.asyncCall = newValue;
        this._dataSource = AutoCompleteAsyncDataSourceImpl.new().initWithOwner(this);
        this._dataSource.currentCompletionMode = CompletionModeImpl.StartsWith;
        this._ios.dataSource = this._dataSource;
    };
    RadAutoCompleteTextView.prototype[commonModule.RadAutoCompleteTextView.suggestionViewProperty.setNative] = function (newValue) {
        var 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();
    };
    RadAutoCompleteTextView.prototype[commonModule.RadAutoCompleteTextView.minimumCharactersToSearchProperty.getDefault] = function () {
        return this._ios.minimumCharactersToSearch;
    };
    RadAutoCompleteTextView.prototype[commonModule.RadAutoCompleteTextView.minimumCharactersToSearchProperty.setNative] = function (newValue) {
        this._ios.minimumCharactersToSearch = newValue;
    };
    RadAutoCompleteTextView.prototype[commonModule.RadAutoCompleteTextView.tokenItemSpacingProperty.getDefault] = function () {
        return this._ios.tokenInset;
    };
    RadAutoCompleteTextView.prototype[commonModule.RadAutoCompleteTextView.tokenItemSpacingProperty.setNative] = function (newValue) {
        this._ios.tokenInset = newValue;
    };
    RadAutoCompleteTextView.prototype[commonModule.RadAutoCompleteTextView.noResultsTextProperty.getDefault] = function () {
        return this._ios.noResultsText;
    };
    RadAutoCompleteTextView.prototype[commonModule.RadAutoCompleteTextView.noResultsTextProperty.setNative] = function (newValue) {
        this._ios.noResultsText = newValue;
    };
    RadAutoCompleteTextView.prototype[commonModule.RadAutoCompleteTextView.showCloseButtonProperty.getDefault] = function () {
        return this._ios.showCloseButton;
    };
    RadAutoCompleteTextView.prototype[commonModule.RadAutoCompleteTextView.showCloseButtonProperty.setNative] = function (newValue) {
        this._ios.showCloseButton = newValue;
    };
    RadAutoCompleteTextView.prototype[commonModule.RadAutoCompleteTextView.hintProperty.getDefault] = function () {
        return this._ios.textField.placeholder;
    };
    RadAutoCompleteTextView.prototype[commonModule.RadAutoCompleteTextView.hintProperty.setNative] = function (newValue) {
        this._ios.textField.placeholder = newValue;
    };
    RadAutoCompleteTextView.prototype[commonModule.RadAutoCompleteTextView.textProperty.getDefault] = function () {
        return this._ios.textField.text;
    };
    RadAutoCompleteTextView.prototype[commonModule.RadAutoCompleteTextView.textProperty.setNative] = function (newValue) {
        if (this._ios.textField.text === newValue) {
            return;
        }
        this._ios.textField.text = newValue;
        this._ios.updateCloseButtonVisibility();
        var args = new commonModule.AutoCompleteEventData(this, commonModule.RadAutoCompleteTextView.textChangedEvent, newValue, undefined);
        this.notify(args);
    };
    RadAutoCompleteTextView.prototype[commonModule.RadAutoCompleteTextView.closeButtonImageSrcProperty.getDefault] = function () {
        return null;
    };
    RadAutoCompleteTextView.prototype[commonModule.RadAutoCompleteTextView.closeButtonImageSrcProperty.setNative] = function (newValue) {
        var imageSrc = this.closeButtonImageSrc;
        var image;
        if (!imageSrc) {
            image = UIImage.new();
        }
        else if (imageSrc.startsWith("res://")) {
            var name_2 = imageSrc.substring(6, imageSrc.length);
            image = UIImage.imageNamed(name_2);
        }
        else {
            image = UIImage.imageNamed(imageSrc);
        }
        this._ios.closeButton.setImageForState(image, 0 /* Normal */);
    };
    RadAutoCompleteTextView.prototype[core_1.colorProperty.getDefault] = function () {
        return null;
    };
    RadAutoCompleteTextView.prototype[core_1.colorProperty.setNative] = function (value) {
        var nativeColor = value instanceof core_1.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);
        }
    };
    RadAutoCompleteTextView.prototype[core_1.fontInternalProperty.getDefault] = function () {
        return this._ios.textField.font;
    };
    RadAutoCompleteTextView.prototype[core_1.fontInternalProperty.setNative] = function (value) {
        var nativeFont = value instanceof core_1.Font ? value.getUIFont(this._ios.textField.font) : value;
        this._ios.textField.font = nativeFont;
        this._ios.appendLabel.font = nativeFont;
    };
    RadAutoCompleteTextView.prototype[core_1.paddingLeftProperty.getDefault] = function () {
        return this._ios.insets.left;
    };
    RadAutoCompleteTextView.prototype[core_1.paddingLeftProperty.setNative] = function (value) {
        var borderLeft = isNaN(+this.style.borderLeftWidth) ? 0 : +this.style.borderLeftWidth;
        var currentInsets = this._ios.insets;
        var insets = new UIEdgeInsets({
            left: value + borderLeft,
            top: currentInsets.top,
            right: currentInsets.right,
            bottom: currentInsets.bottom
        });
        this._ios.insets = insets;
    };
    RadAutoCompleteTextView.prototype[core_1.borderLeftWidthProperty.getDefault] = function () {
        return 0;
    };
    RadAutoCompleteTextView.prototype[core_1.borderLeftWidthProperty.setNative] = function (value) {
        var paddingLeft = isNaN(+this.style.paddingLeft) ? 0 : +this.style.paddingLeft;
        var currentInsets = this._ios.insets;
        var insets = new UIEdgeInsets({
            left: value + paddingLeft,
            top: currentInsets.top,
            right: currentInsets.right,
            bottom: currentInsets.bottom
        });
        this._ios.insets = insets;
    };
    RadAutoCompleteTextView.prototype[core_1.paddingTopProperty.getDefault] = function () {
        return this._ios.insets.top;
    };
    RadAutoCompleteTextView.prototype[core_1.paddingTopProperty.setNative] = function (value) {
        var borderTop = isNaN(+this.style.borderTopWidth) ? 0 : +this.style.borderTopWidth;
        var currentInsets = this._ios.insets;
        var insets = new UIEdgeInsets({
            left: currentInsets.left,
            top: value + borderTop,
            right: currentInsets.right,
            bottom: currentInsets.bottom
        });
        this._ios.insets = insets;
    };
    RadAutoCompleteTextView.prototype[core_1.borderTopWidthProperty.getDefault] = function () {
        return 0;
    };
    RadAutoCompleteTextView.prototype[core_1.borderTopWidthProperty.setNative] = function (value) {
        var paddingTop = isNaN(+this.style.paddingTop) ? 0 : +this.style.paddingTop;
        var currentInsets = this._ios.insets;
        var insets = new UIEdgeInsets({
            left: currentInsets.left,
            top: value + paddingTop,
            right: currentInsets.right,
            bottom: currentInsets.bottom
        });
        this._ios.insets = insets;
    };
    RadAutoCompleteTextView.prototype[core_1.paddingRightProperty.getDefault] = function () {
        return this._ios.insets.right;
    };
    RadAutoCompleteTextView.prototype[core_1.paddingRightProperty.setNative] = function (value) {
        var borderRight = isNaN(+this.style.borderRightWidth) ? 0 : +this.style.borderRightWidth;
        var currentInsets = this._ios.insets;
        var insets = new UIEdgeInsets({
            left: currentInsets.left,
            top: currentInsets.top,
            right: value + borderRight,
            bottom: currentInsets.bottom
        });
        this._ios.insets = insets;
    };
    RadAutoCompleteTextView.prototype[core_1.borderRightWidthProperty.getDefault] = function () {
        return 0;
    };
    RadAutoCompleteTextView.prototype[core_1.borderRightWidthProperty.setNative] = function (value) {
        var paddingRight = isNaN(+this.style.paddingRight) ? 0 : +this.style.paddingRight;
        var currentInsets = this._ios.insets;
        var insets = new UIEdgeInsets({
            left: currentInsets.left,
            top: currentInsets.top,
            right: value + paddingRight,
            bottom: currentInsets.bottom
        });
        this._ios.insets = insets;
    };
    RadAutoCompleteTextView.prototype[core_1.paddingBottomProperty.getDefault] = function () {
        return this._ios.insets.bottom;
    };
    RadAutoCompleteTextView.prototype[core_1.paddingBottomProperty.setNative] = function (value) {
        var borderBottom = isNaN(+this.style.borderBottomWidth) ? 0 : +this.style.borderBottomWidth;
        var currentInsets = this._ios.insets;
        var insets = new UIEdgeInsets({
            left: currentInsets.left,
            top: currentInsets.top,
            right: currentInsets.right,
            bottom: value + borderBottom,
        });
        this._ios.insets = insets;
    };
    RadAutoCompleteTextView.prototype[core_1.borderBottomWidthProperty.getDefault] = function () {
        return 0;
    };
    RadAutoCompleteTextView.prototype[core_1.borderBottomWidthProperty.setNative] = function (value) {
        var paddingBottom = isNaN(+this.style.paddingBottom) ? 0 : +this.style.paddingBottom;
        var currentInsets = this._ios.insets;
        var insets = new UIEdgeInsets({
            left: currentInsets.left,
            top: currentInsets.top,
            right: currentInsets.right,
            bottom: value + paddingBottom
        });
        this._ios.insets = insets;
    };
    RadAutoCompleteTextView.prototype.tokenModelWithText = function (text) {
        if (this.items) {
            for (var i = 0; i < this.items.length; i++) {
                var current = this.items.getItem(i);
                if (current.text === text) {
                    return current;
                }
            }
        }
        return null;
    };
    RadAutoCompleteTextView.prototype[commonModule.RadAutoCompleteTextView.readOnlyProperty.getDefault] = function () {
        return this._ios.readOnly;
    };
    RadAutoCompleteTextView.prototype[commonModule.RadAutoCompleteTextView.readOnlyProperty.setNative] = function (newValue) {
        this._ios.readOnly = newValue;
    };
    RadAutoCompleteTextView.prototype.adjustCompletionMode = function (value) {
        if (this._ios && value && this._dataSource) {
            if (value === commonModule.AutoCompleteCompletionMode.StartsWith) {
                this._dataSource.currentCompletionMode = CompletionModeImpl.StartsWith;
            }
            else {
                this._dataSource.currentCompletionMode = CompletionModeImpl.Contains;
            }
        }
    };
    RadAutoCompleteTextView.prototype.adjustDisplayMode = function (value) {
        if (this._ios && value) {
            this._ios.displayMode = (value === commonModule.AutoCompleteDisplayMode.Plain) ?
                0 /* Plain */ :
                1 /* Tokens */;
        }
    };
    RadAutoCompleteTextView.prototype.adjustSuggestMode = function (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 */;
            }
        }
    };
    RadAutoCompleteTextView.prototype.adjustLayoutMode = function (value) {
        if (value && this._ios) {
            this._ios.layoutMode = (value === commonModule.AutoCompleteLayoutMode.Horizontal) ?
                0 /* Horizontal */ :
                1 /* Wrap */;
            this.requestLayout();
        }
    };
    return RadAutoCompleteTextView;
}(commonModule.RadAutoCompleteTextView));
exports.RadAutoCompleteTextView = RadAutoCompleteTextView;
var Token = /** @class */ (function (_super) {
    __extends(Token, _super);
    function Token(nativeView) {
        var _this = _super.call(this) || this;
        _this._ios = nativeView;
        _this._clearButton = new TokenClearButton(_this._ios);
        _this._addView(_this._clearButton);
        return _this;
    }
    Token.prototype._updateAutoCompleteTokenSelectedHandlers = function (subscribe) {
        this._ios.useHighlighting = !subscribe;
    };
    Token.prototype.createNativeView = function () {
        return this._ios;
    };
    Token.prototype.disposeNativeView = function () {
        this._ios = null;
    };
    Object.defineProperty(Token.prototype, "clearButton", {
        get: function () {
            return this._clearButton;
        },
        enumerable: false,
        configurable: true
    });
    Token.prototype.eachChild = function (callback) {
        if (this.clearButton) {
            callback(this.clearButton);
        }
    };
    Token.prototype[core_1.colorProperty.getDefault] = function () {
        return this._ios.textLabel.textColor;
    };
    Token.prototype[core_1.colorProperty.setNative] = function (value) {
        var nativeColor = value instanceof core_1.Color ? value.ios : value;
        this._ios.textLabel.textColor = nativeColor;
    };
    Token.prototype[core_1.fontInternalProperty.getDefault] = function () {
        return this._ios.textLabel.font;
    };
    Token.prototype[core_1.fontInternalProperty.setNative] = function (value) {
        var nativeFont = value instanceof core_1.Font ? value.getUIFont(this._ios.textLabel.font) : value;
        this._ios.textLabel.font = nativeFont;
    };
    Token.prototype[core_1.paddingLeftProperty.getDefault] = function () {
        return this._ios.insets.left;
    };
    Token.prototype[core_1.paddingLeftProperty.setNative] = function (value) {
        var borderLeft = isNaN(+this.style.borderLeftWidth) ? 0 : +this.style.borderLeftWidth;
        var currentInsets = this._ios.insets;
        var insets = new UIEdgeInsets({
            left: value + borderLeft,
            top: currentInsets.top,
            right: currentInsets.right,
            bottom: currentInsets.bottom
        });
        this._ios.insets = insets;
    };
    Token.prototype[core_1.borderLeftWidthProperty.getDefault] = function () {
        return 0;
    };
    Token.prototype[core_1.borderLeftWidthProperty.setNative] = function (value) {
        var paddingLeft = isNaN(+this.style.paddingLeft) ? 0 : +this.style.paddingLeft;
        var currentInsets = this._ios.insets;
        var insets = new UIEdgeInsets({
            left: value + paddingLeft,
            top: currentInsets.top,
            right: currentInsets.right,
            bottom: currentInsets.bottom
        });
        this._ios.insets = insets;
    };
    Token.prototype[core_1.paddingTopProperty.getDefault] = function () {
        return this._ios.insets.top;
    };
    Token.prototype[core_1.paddingTopProperty.setNative] = function (value) {
        var borderTop = isNaN(+this.style.borderTopWidth) ? 0 : +this.style.borderTopWidth;
        var currentInsets = this._ios.insets;
        var insets = new UIEdgeInsets({
            left: currentInsets.left,
            top: value + borderTop,
            right: currentInsets.right,
            bottom: currentInsets.bottom
        });
        this._ios.insets = insets;
    };
    Token.prototype[core_1.borderTopWidthProperty.getDefault] = function () {
        return 0;
    };
    Token.prototype[core_1.borderTopWidthProperty.setNative] = function (value) {
        var paddingTop = isNaN(+this.style.paddingTop) ? 0 : +this.style.paddingTop;
        var currentInsets = this._ios.insets;
        var insets = new UIEdgeInsets({
            left: currentInsets.left,
            top: value + paddingTop,
            right: currentInsets.right,
            bottom: currentInsets.bottom
        });
        this._ios.insets = insets;
    };
    Token.prototype[core_1.paddingRightProperty.getDefault] = function () {
        return this._ios.insets.right;
    };
    Token.prototype[core_1.paddingRightProperty.setNative] = function (value) {
        var borderRight = isNaN(+this.style.borderRightWidth) ? 0 : +this.style.borderRightWidth;
        var currentInsets = this._ios.insets;
        var insets = new UIEdgeInsets({
            left: currentInsets.left,
            top: currentInsets.top,
            right: value + borderRight,
            bottom: currentInsets.bottom
        });
        this._ios.insets = insets;
    };
    Token.prototype[core_1.borderRightWidthProperty.getDefault] = function () {
        return 0;
    };
    Token.prototype[core_1.borderRightWidthProperty.setNative] = function (value) {
        var paddingRight = isNaN(+this.style.paddingRight) ? 0 : +this.style.paddingRight;
        var currentInsets = this._ios.insets;
        var insets = new UIEdgeInsets({
            left: currentInsets.left,
            top: currentInsets.top,
            right: value + paddingRight,
            bottom: currentInsets.bottom
        });
        this._ios.insets = insets;
    };
    Token.prototype[core_1.paddingBottomProperty.getDefault] = function () {
        return this._ios.insets.bottom;
    };
    Token.prototype[core_1.paddingBottomProperty.setNative] = function (value) {
        var borderBottom = isNaN(+this.style.borderBottomWidth) ? 0 : +this.style.borderBottomWidth;
        var currentInsets = this._ios.insets;
        var insets = new UIEdgeInsets({
            left: currentInsets.left,
            top: currentInsets.top,
            right: currentInsets.right,
            bottom: value + borderBottom,
        });
        this._ios.insets = insets;
    };
    Token.prototype[core_1.borderBottomWidthProperty.getDefault] = function () {
        return 0;
    };
    Token.prototype[core_1.borderBottomWidthProperty.setNative] = function (value) {
        var paddingBottom = isNaN(+this.style.paddingBottom) ? 0 : +this.style.paddingBottom;
        var currentInsets = this._ios.insets;
        var insets = new UIEdgeInsets({
            left: currentInsets.left,
            top: currentInsets.top,
            right: currentInsets.right,
            bottom: value + paddingBottom
        });
        this._ios.insets = insets;
    };
    __decorate([
        core_1.PseudoClassHandler("selected", "normal"),
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Boolean]),
        __metadata("design:returntype", void 0)
    ], Token.prototype, "_updateAutoCompleteTokenSelectedHandlers", null);
    return Token;
}(commonModule.Token));
exports.Token = Token;
var NoResultsLabel = /** @class */ (function (_super) {
    __extends(NoResultsLabel, _super);
    function NoResultsLabel(nativeView) {
        var _this = _super.call(this) || this;
        _this._ios = nativeView;
        return _this;
    }
    NoResultsLabel.prototype.createNativeView = function () {
        return this._ios;
    };
    NoResultsLabel.prototype.disposeNativeView = function () {
        this._ios = null;
    };
    NoResultsLabel.prototype[core_1.colorProperty.getDefault] = function () {
        return null;
    };
    NoResultsLabel.prototype[core_1.colorProperty.setNative] = function (value) {
        var nativeColor = value instanceof core_1.Color ? value.ios : value;
        if (nativeColor) {
            this._ios.textColor = nativeColor;
        }
        else {
            this._ios.textColor = UIColor.blackColor;
        }
    };
    NoResultsLabel.prototype[core_1.fontInternalProperty.getDefault] = function () {
        return this._ios.font;
    };
    NoResultsLabel.prototype[core_1.fontInternalProperty.setNative] = function (value) {
        var nativeFont = value instanceof core_1.Font ? value.getUIFont(this._ios.font) : value;
        this._ios.font = nativeFont;
    };
    return NoResultsLabel;
}(commonModule.NoResultsLabel));
exports.NoResultsLabel = NoResultsLabel;
var ClearButton = /** @class */ (function (_super) {
    __extends(ClearButton, _super);
    function ClearButton(owner) {
        var _this = _super.call(this) || this;
        _this._owner = owner;
        _this._ios = owner.closeButton;
        return _this;
    }
    ClearButton.prototype.createNativeView = function () {
        return this._ios;
    };
    ClearButton.prototype.disposeNativeView = function () {
        this._ios = null;
    };
    ClearButton.prototype[core_1.colorProperty.getDefault] = function () {
        return null;
    };
    ClearButton.prototype[core_1.colorProperty.setNative] = function (value) {
        var nativeColor = value instanceof core_1.Color ? value.ios : value;
        this._ios.circleBackgroundColor = nativeColor;
        this._ios.tintColor = nativeColor;
    };
    ClearButton.prototype[core_1.widthProperty.getDefault] = function () {
        return this._owner.closeButtonWidth;
    };
    ClearButton.prototype[core_1.widthProperty.setNative] = function (value) {
        this._owner.closeButtonWidth = value;
    };
    ClearButton.prototype[core_1.heightProperty.getDefault] = function () {
        return this._owner.closeButtonWidth;
    };
    ClearButton.prototype[core_1.heightProperty.setNative] = function (value) {
        this._owner.closeButtonHeight = value;
    };
    ClearButton.prototype[core_1.backgroundImageProperty.getDefault] = function () {
        return null;
    };
    ClearButton.prototype[core_1.backgroundImageProperty.setNative] = function (value) {
        this._ios.drawShape = (value === null);
    };
    return ClearButton;
}(commonModule.ClearButton));
exports.ClearButton = ClearButton;
var TokenClearButton = /** @class */ (function (_super) {
    __extends(TokenClearButton, _super);
    function TokenClearButton(token) {
        var _this = _super.call(this) || this;
        _this._token = token;
        _this._ios = token.removeButton;
        return _this;
    }
    TokenClearButton.prototype.createNativeView = function () {
        return this._ios;
    };
    TokenClearButton.prototype.disposeNativeView = function () {
        this._ios = null;
    };
    TokenClearButton.prototype[core_1.colorProperty.getDefault] = function () {
        return this._ios.tintColor;
    };
    TokenClearButton.prototype[core_1.colorProperty.setNative] = function (value) {
        var nativeColor = value instanceof core_1.Color ? value.ios : value;
        this._ios.tintColor = nativeColor;
        this._ios.setNeedsDisplay();
    };
    TokenClearButton.prototype[core_1.backgroundImageProperty.getDefault] = function () {
        return null;
    };
    TokenClearButton.prototype[core_1.backgroundImageProperty.setNative] = function (value) {
        this._ios.drawShape = (value === null);
        this._ios.setNeedsDisplay();
    };
    TokenClearButton.prototype[core_1.widthProperty.getDefault] = function () {
        return this._token.removeButtonWidth;
    };
    TokenClearButton.prototype[core_1.widthProperty.setNative] = function (value) {
        this._token.removeButtonWidth = value;
    };
    TokenClearButton.prototype[core_1.heightProperty.getDefault] = function () {
        return this._token.removeButtonHeight;
    };
    TokenClearButton.prototype[core_1.heightProperty.setNative] = function (value) {
        this._token.removeButtonHeight = value;
    };
    return TokenClearButton;
}(commonModule.TokenClearButton));
exports.TokenClearButton = TokenClearButton;
function getPositionFromFrame(frame) {
    var left = core_1.Utils.layout.round(core_1.Utils.layout.toDevicePixels(frame.origin.x));
    var top = core_1.Utils.layout.round(core_1.Utils.layout.toDevicePixels(frame.origin.y));
    var right = core_1.Utils.layout.round(core_1.Utils.layout.toDevicePixels(frame.origin.x + frame.size.width));
    var bottom = core_1.Utils.layout.round(core_1.Utils.layout.toDevicePixels(frame.origin.y + frame.size.height));
    return { left: left, right: right, top: top, bottom: bottom };
}
