define(["require", "exports", "tslib", "react", "office-ui-fabric-react/lib/Button", "@uifabric/example-data", "@uifabric/experiments/lib/SelectedItemsList", "@uifabric/experiments/lib/FloatingPeopleSuggestions", "@uifabric/experiments/lib/FloatingSuggestions"], function (require, exports, tslib_1, React, Button_1, example_data_1, SelectedItemsList_1, FloatingPeopleSuggestions_1, FloatingSuggestions_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var SelectedPeopleListWithEditExample = /** @class */ (function (_super) {
        tslib_1.__extends(SelectedPeopleListWithEditExample, _super);
        function SelectedPeopleListWithEditExample(props) {
            var _this = _super.call(this, props) || this;
            // Used to resolve suggestions on the editableItem
            _this.model = new ExampleSuggestionsModel(example_data_1.people);
            _this.suggestionsStore = new FloatingSuggestions_1.SuggestionsStore();
            /**
             * Build a custom selected item capable of being edited when the item is right clicked
             */
            _this.SelectedItem = SelectedItemsList_1.EditableItem({
                itemComponent: SelectedItemsList_1.TriggerOnContextMenu(SelectedItemsList_1.SelectedPersona),
                editingItemComponent: SelectedItemsList_1.DefaultEditingItem({
                    getEditingItemText: function (persona) { return persona.text || ''; },
                    onRenderFloatingPicker: function (props) { return (React.createElement(FloatingPeopleSuggestions_1.FloatingPeopleSuggestions, tslib_1.__assign({}, props, { suggestionsStore: _this.suggestionsStore, onResolveSuggestions: _this.model.resolveSuggestions }))); },
                }),
            });
            _this._onAddItemButtonClicked = function () {
                var randomPerson = example_data_1.people[Math.floor(Math.random() * (example_data_1.people.length - 1))];
                _this.setState({ currentSelectedItems: tslib_1.__spreadArrays(_this.state.currentSelectedItems, [randomPerson]) });
            };
            _this._onItemsRemoved = function (items) {
                var currentSelectedItemsCopy = tslib_1.__spreadArrays(_this.state.currentSelectedItems);
                items.forEach(function (item) {
                    var indexToRemove = currentSelectedItemsCopy.indexOf(item);
                    currentSelectedItemsCopy.splice(indexToRemove, 1);
                    _this.setState({ currentSelectedItems: tslib_1.__spreadArrays(currentSelectedItemsCopy) });
                });
            };
            _this.state = {
                currentSelectedItems: [example_data_1.people[40]],
            };
            return _this;
        }
        SelectedPeopleListWithEditExample.prototype.render = function () {
            return (React.createElement("div", { className: 'ms-BasePicker-text' },
                "Right click any persona to edit it",
                React.createElement("br", null),
                React.createElement(Button_1.PrimaryButton, { text: "Add another item", onClick: this._onAddItemButtonClicked }),
                this._renderExtendedPicker()));
        };
        SelectedPeopleListWithEditExample.prototype._renderExtendedPicker = function () {
            return (React.createElement("div", null,
                React.createElement(SelectedItemsList_1.SelectedPeopleList, { key: 'normal', removeButtonAriaLabel: 'Remove', selectedItems: tslib_1.__spreadArrays(this.state.currentSelectedItems), onRenderItem: this.SelectedItem, onItemsRemoved: this._onItemsRemoved })));
        };
        return SelectedPeopleListWithEditExample;
    }(React.Component));
    exports.SelectedPeopleListWithEditExample = SelectedPeopleListWithEditExample;
    var ExampleSuggestionsModel = /** @class */ (function () {
        function ExampleSuggestionsModel(data) {
            var _this = this;
            this.resolveSuggestions = function (filterText, currentItems) {
                var filteredItems = [];
                if (filterText) {
                    filteredItems = _this._filterItemsByText(filterText);
                    filteredItems = _this._removeDuplicates(filteredItems, currentItems || []);
                }
                return _this._convertResultsToPromise(filteredItems);
            };
            this.suggestionsData = tslib_1.__spreadArrays(data);
        }
        ExampleSuggestionsModel.prototype.removeSuggestion = function (item) {
            var index = this.suggestionsData.indexOf(item);
            console.log('removing', item, 'at', index);
            if (index !== -1) {
                this.suggestionsData.splice(index, 1);
            }
        };
        ExampleSuggestionsModel.prototype._filterItemsByText = function (filterText) {
            var _this = this;
            return this.suggestionsData.filter(function (item) {
                var itemText = item.text || item.name;
                return itemText ? _this._doesTextStartWith(itemText, filterText) : false;
            });
        };
        ExampleSuggestionsModel.prototype._doesTextStartWith = function (text, filterText) {
            return text.toLowerCase().indexOf(filterText.toLowerCase()) === 0;
        };
        ExampleSuggestionsModel.prototype._removeDuplicates = function (items, possibleDupes) {
            var _this = this;
            return items.filter(function (item) { return !_this._listContainsItem(item, possibleDupes); });
        };
        ExampleSuggestionsModel.prototype._listContainsItem = function (item, Items) {
            if (!Items || !Items.length || Items.length === 0) {
                return false;
            }
            return Items.filter(function (i) { return (i.text || i.name) === (item.text || item.name); }).length > 0;
        };
        ExampleSuggestionsModel.prototype._convertResultsToPromise = function (results) {
            return new Promise(function (resolve) { return setTimeout(function () { return resolve(results); }, 150); });
        };
        return ExampleSuggestionsModel;
    }());
});
//# sourceMappingURL=SelectedPeopleList.WithEdit.Example.js.map