var compare = require('natural-orderby').compare;
var errors = require("./errors.js");
import { FreeDatas2HTML } from "./FreeDatas2HTML";
var Selector = (function () {
function Selector(converter, datasFieldNb, elt, separator) {
this._datasViewElt = { id: "", eltDOM: undefined };
this._selectedValue = undefined;
this._values = [];
this._name = "";
if (converter.fields.length === 0 || converter.datas.length === 0)
throw new Error(errors.filterNeedDatas);
else if (!converter.checkFieldExist(Number(datasFieldNb)))
throw new Error(errors.selectorFieldNotFound);
else {
this._datasViewElt = FreeDatas2HTML.checkInDOMById(elt);
this._converter = converter;
this._datasFieldNb = datasFieldNb;
if (separator !== undefined && separator !== "")
this._separator = separator;
}
}
Object.defineProperty(Selector.prototype, "converter", {
get: function () {
return this._converter;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Selector.prototype, "datasViewElt", {
get: function () {
return this._datasViewElt;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Selector.prototype, "datasFieldNb", {
get: function () {
return this._datasFieldNb;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Selector.prototype, "name", {
get: function () {
return this._name;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Selector.prototype, "selectedValue", {
get: function () {
return this._selectedValue;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Selector.prototype, "separator", {
get: function () {
return this._separator;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Selector.prototype, "values", {
get: function () {
return this._values;
},
enumerable: true,
configurable: true
});
Selector.prototype.filter2HTML = function (label) {
if (label === void 0) { label = ""; }
this._name = this._converter.fields[this._datasFieldNb];
for (var _i = 0, _a = this._converter.datas; _i < _a.length; _i++) {
var row = _a[_i];
var checkedValue = void 0;
if (this._separator === undefined) {
checkedValue = row[this._name].trim();
if (checkedValue !== "" && this._values.indexOf(checkedValue) === -1)
this._values.push(checkedValue);
}
else {
var checkedValues = row[this._name].split(this._separator);
for (var _b = 0, checkedValues_1 = checkedValues; _b < checkedValues_1.length; _b++) {
var value = checkedValues_1[_b];
checkedValue = value.trim();
if (checkedValue !== "" && this._values.indexOf(checkedValue) === -1)
this._values.push(checkedValue);
}
}
}
if (this._values.length === 0)
throw new Error(errors.selectorFieldIsEmpty);
else {
if (this._converter.getSortingFunctionForField(this._datasFieldNb) !== undefined)
this._values.sort(this._converter.getSortingFunctionForField(this._datasFieldNb).sort);
else
this._values.sort(compare());
label = (label === "") ? this._name : label;
var selectorsHTML = "";
this._datasViewElt.eltDOM.innerHTML = selectorsHTML;
var selectElement_1 = document.getElementById("freeDatas2HTML_" + this._datasViewElt.id), mySelector_1 = this;
selectElement_1.addEventListener("change", function (e) {
if (selectElement_1.value === "0")
mySelector_1._selectedValue = undefined;
else
mySelector_1._selectedValue = parseInt(selectElement_1.value, 10) - 1;
mySelector_1._converter.refreshView();
});
}
};
Selector.prototype.dataIsOk = function (data) {
if (this._name === "")
throw new Error(errors.filterCheckIsOkFail);
if (this._selectedValue === undefined)
return true;
if (this._values[this._selectedValue] === undefined)
throw new Error(errors.selectorSelectedIndexNotFound);
if (data[this._name] === undefined)
return false;
var selectedValueTxt = this._values[this._selectedValue];
if (this._separator === undefined) {
if (data[this._name].trim() !== selectedValueTxt)
return false;
else
return true;
}
else {
var find = false;
var checkedValues = data[this._name].split(this._separator);
for (var _i = 0, checkedValues_2 = checkedValues; _i < checkedValues_2.length; _i++) {
var value = checkedValues_2[_i];
if (value.trim() === selectedValueTxt) {
find = true;
break;
}
}
return find;
}
};
return Selector;
}());
export { Selector };