"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
var _ = require("lodash");
var React = require("react");
var default_1 = /** @class */ (function (_super) {
    __extends(default_1, _super);
    function default_1(props) {
        var _this = _super.call(this, props) || this;
        _this.nameField = 'name';
        _this.nameFormat = function (v) { return v; };
        _this.valueField = '';
        _this.valueFormat = function (v) { return v; };
        _this.hasOnSelected = false;
        _this.doubled = false;
        _this.className = '';
        _this.state = {
            currentSelected: '',
            data: [],
        };
        if (_.has(_this.props, 'nameField')) {
            _this.nameField = _this.props.nameField;
        }
        if (_.has(_this.props, 'nameFormat')) {
            _this.nameFormat = _this.props.nameFormat;
        }
        var className = ['selection-list'];
        if (_.has(_this.props, 'valueField')) {
            _this.valueField = _this.props.valueField;
            className.push('selection-list-valued');
        }
        if (_.has(_this.props, 'valueFormat')) {
            _this.valueFormat = _this.props.valueFormat;
        }
        if (_this.props.onSelect) {
            _this.hasOnSelected = true;
            className.push('selection-list-select');
        }
        if (_this.props.double) {
            _this.doubled = true;
            className.push('selection-list-doubled');
        }
        _this.className = className.join(' ');
        return _this;
    }
    default_1.prototype.componentWillReceiveProps = function (props) {
        if (this.props.data.length != props.data.length) {
            if (this.valueField && this.props.sorted) {
                this.setState({ data: _.orderBy(props.data, this.valueField, 'desc') });
            }
            else {
                this.setState({ data: props.data });
            }
        }
    };
    default_1.prototype.componentDidMount = function () {
        if (this.valueField && this.props.sorted) {
            this.setState({ data: _.orderBy(this.props.data, this.valueField, 'desc') });
        }
        else {
            this.setState({ data: this.props.data });
        }
    };
    default_1.prototype.renderItem = function (item) {
        var _this = this;
        var currentSelected = this.state.currentSelected;
        var param = {};
        if (currentSelected == item[this.nameField]) {
            param.className = 'selection-item-select';
        }
        if (this.hasOnSelected) {
            param.onClick = function () {
                if (item[_this.nameField] == _this.state.currentSelected) {
                    _this.setState({ currentSelected: null });
                    _this.props.onSelect(null);
                }
                else {
                    _this.setState({ currentSelected: item[_this.nameField] });
                    _this.props.onSelect(item);
                }
            };
        }
        return React.createElement("li", __assign({}, param),
            React.createElement("span", { className: "selection-item-label" }, this.nameFormat(item[this.nameField])),
            this.valueField && React.createElement("span", { className: "selection-item-value" }, this.valueFormat(item[this.valueField])));
    };
    default_1.prototype.render = function () {
        var _this = this;
        return React.createElement("ul", { className: this.className }, this.state.data.map(function (item) { return _this.renderItem(item); }));
    };
    return default_1;
}(React.PureComponent));
exports.default = default_1;
