"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Types = exports.TypeNone = exports.TypeFile = exports.TypeListBinary = exports.TypeList = exports.TypeZoom = exports.TypeRanges = exports.TypeFloat = exports.TypeNumber = exports.TypeStacking = exports.TypeCheckbox = exports.TypeBool = exports.TypeTextarea = exports.TypeJSON = exports.TypeText = exports.TypeButton = exports.TypeSelectTableQuery = exports.TypeSelectPolicy = exports.TypeSelectRole = exports.TypeSelectConfiguration = exports.TypeEnum = void 0;
var tslib_1 = require("tslib");
var react_1 = tslib_1.__importStar(require("react"));
var v_eris_1 = require("v-eris");
var types_ranges_1 = require("./types.ranges");
var types_zoom_1 = require("./types.zoom");
var types_table_1 = require("./types.table");
var types_theme_1 = require("./types.theme");
require("./types.scss");
var use_storage_1 = require("../utility/use.storage");
var types_appendix_1 = require("./types.appendix");
var TypeEnum = function (props) {
    var params = props.params, paramsOverride = props.paramsOverride, icon = props.icon, value = props.value, lang = props.lang, inactive = props.inactive, extra = props.extra, settings = props.settings, secure = props.secure, feedback = props.feedback, rest = tslib_1.__rest(props, ["params", "paramsOverride", "icon", "value", "lang", "inactive", "extra", "settings", "secure", "feedback"]);
    if (!params)
        params = {};
    if (!extra)
        extra = {};
    var list = paramsOverride || props.params || [];
    if (!Array.isArray(list))
        list = [];
    (0, react_1.useEffect)(function () {
        if (!feedback)
            return;
        feedback(value === undefined && list[0] ? (list[0].token || list[0].value || "") : value, list);
    }, [list]);
    var secureList = settings && secure ? settings.getSecureValue(secure) : null;
    if (secureList && secureList.list)
        secureList = secureList.list;
    else
        secureList = null;
    return (react_1.default.createElement(v_eris_1.Select, tslib_1.__assign({ value: value, inactive: inactive, icon: icon, list: (secureList || list).map(function (item) {
            if (secureList)
                item = list.find(function (f) { return f.value == item; });
            if (item && (item.value || item.token || item.title))
                item = { value: item.token || item.value || "", title: item.title || item.name || "", icon: item.icon };
            else
                item = { value: item || "", title: item || "" };
            item.title = lang.get(item.title);
            return { icon: item.icon || icon, value: item.value, title: item.title };
        }), onChange: props.onChange }, extra)));
};
exports.TypeEnum = TypeEnum;
var TypeSelectConfiguration = function (props) {
    var routeStorage = (0, react_1.useContext)(use_storage_1.StorageContext);
    var paramsOverride = routeStorage.getValue("configurations") || [];
    return react_1.default.createElement(exports.TypeEnum, tslib_1.__assign({}, props, { paramsOverride: paramsOverride }));
};
exports.TypeSelectConfiguration = TypeSelectConfiguration;
var TypeSelectRole = function (props) {
    var routeStorage = (0, react_1.useContext)(use_storage_1.StorageContext);
    var paramsOverride = routeStorage.getValue("roles") || [];
    var configurations = routeStorage.getValue("configurations") || [];
    var params = props.params || {};
    var filter = params.filter;
    var where = (0, react_1.useMemo)(function () {
        return (0, types_appendix_1.ParseWhere)(filter, props.storage);
    }, [filter, routeStorage.it, props.storage ? props.storage.it : 0]);
    if (where.configuration)
        paramsOverride = paramsOverride.filter(function (f) { return f.configuration == where.configuration; });
    var _loop_1 = function (item) {
        var configuration = configurations.find(function (f) { return f.token == item.configuration; });
        item.title = (configuration ? configuration.name : "default") + "." + item.name;
    };
    for (var _i = 0, paramsOverride_1 = paramsOverride; _i < paramsOverride_1.length; _i++) {
        var item = paramsOverride_1[_i];
        _loop_1(item);
    }
    ;
    return react_1.default.createElement(exports.TypeEnum, tslib_1.__assign({}, props, { paramsOverride: paramsOverride }));
};
exports.TypeSelectRole = TypeSelectRole;
var TypeSelectPolicy = function (props) {
    var routeStorage = (0, react_1.useContext)(use_storage_1.StorageContext);
    var paramsOverride = routeStorage.getValue("policy") || [];
    return react_1.default.createElement(exports.TypeEnum, tslib_1.__assign({}, props, { paramsOverride: paramsOverride }));
};
exports.TypeSelectPolicy = TypeSelectPolicy;
var TypeSelectTableQuery = function (props) {
    var routeStorage = (0, react_1.useContext)(use_storage_1.StorageContext);
    var params = props.params || {};
    var filter = params.filter;
    var route = params.route || "";
    var where = (0, react_1.useMemo)(function () {
        return (0, types_appendix_1.ParseWhere)(filter, routeStorage);
    }, [route, filter, routeStorage.it]);
    var storage = (0, use_storage_1.useStorage)({
        where: where,
        section: route,
        current: false
    });
    (0, v_eris_1.useDelta)(function () {
        if (filter && Object.keys(filter).length && !Object.keys(where).length)
            return;
        storage.async.list.fetch();
    }, [route, where], where);
    var paramsOverride = params.none ? tslib_1.__spreadArray([{ key: "", token: "none", name: "None" }], storage.list || [], true) : storage.list;
    return react_1.default.createElement(exports.TypeEnum, tslib_1.__assign({}, props, { paramsOverride: paramsOverride }));
};
exports.TypeSelectTableQuery = TypeSelectTableQuery;
var TypeButton = function (props) {
    var params = props.params, paramsOverride = props.paramsOverride, value = props.value, lang = props.lang, rest = tslib_1.__rest(props, ["params", "paramsOverride", "value", "lang"]);
    if (!params)
        params = {};
    if (!paramsOverride)
        paramsOverride = {};
    return (react_1.default.createElement("div", { className: "settings-type-button" },
        react_1.default.createElement(v_eris_1.Button, { onClick: function (e) {
                if (paramsOverride.onChange)
                    paramsOverride.onChange(e.event, props);
            } }, lang.get(params.title))));
};
exports.TypeButton = TypeButton;
var TypeText = function (props) {
    var params = props.params, value = props.value, lang = props.lang, inactive = props.inactive, rest = tslib_1.__rest(props, ["params", "value", "lang", "inactive"]);
    if (!params)
        params = {};
    var conditions = Array.isArray(params.conditions) ?
        params.conditions.map(function (item) {
            var _a;
            return tslib_1.__assign(tslib_1.__assign({}, item), (_a = {}, _a["desc"] = lang.get(item.desc), _a));
        })
        :
            [];
    return (react_1.default.createElement("div", { className: "settings-type-text" },
        react_1.default.createElement(v_eris_1.Input, tslib_1.__assign({ inactive: inactive }, params, { conditions: conditions, onChange: function (e) { props.onChange({ value: e.value }); } }), value === 0 ? "0" : value)));
};
exports.TypeText = TypeText;
var TypeJSON = function (props) {
    var params = props.params, value = props.value, lang = props.lang, inactive = props.inactive, rest = tslib_1.__rest(props, ["params", "value", "lang", "inactive"]);
    if (!params)
        params = {};
    var conditions = Array.isArray(params.conditions) ?
        params.conditions.map(function (item) {
            var _a;
            return tslib_1.__assign(tslib_1.__assign({}, item), (_a = {}, _a["desc"] = lang.get(item.desc), _a));
        })
        :
            [];
    try {
        if (typeof value == "string") {
            //let object = JSON.parse( value );
            //value = JSON.stringify( object, null, 3 );
        }
        else {
            value = JSON.stringify(value, null, 3);
        }
        ;
    }
    catch (e) {
        value = typeof value == "string" ? value : "";
    }
    ;
    if (params.editable) {
        return (react_1.default.createElement(v_eris_1.Editable, { value: value === 0 ? "0" : value, stretch: params.stretch, conditions: conditions, inactive: inactive, onChange: function (e) { props.onChange({ value: e.target.value }); } }));
    }
    ;
    return (react_1.default.createElement("div", { className: v_eris_1.Props.className("settings-type-code", { small: params.small }) },
        react_1.default.createElement("div", null, value || "")));
};
exports.TypeJSON = TypeJSON;
var TypeTextarea = function (props) {
    var params = props.params, value = props.value, inactive = props.inactive, lang = props.lang, rest = tslib_1.__rest(props, ["params", "value", "inactive", "lang"]);
    if (!params)
        params = {};
    var conditions = Array.isArray(params.conditions) ?
        params.conditions.map(function (item) {
            var _a;
            return tslib_1.__assign(tslib_1.__assign({}, item), (_a = {}, _a["desc"] = lang.get(item.desc), _a));
        })
        :
            [];
    return (react_1.default.createElement(v_eris_1.Editable, { value: value === 0 ? "0" : value, inactive: inactive, stretch: params.stretch, conditions: conditions, onChange: function (e) { props.onChange({ value: e.target.value }); } }));
};
exports.TypeTextarea = TypeTextarea;
var TypeBool = function (props) {
    var params = props.params, value = props.value, inactive = props.inactive, rest = tslib_1.__rest(props, ["params", "value", "inactive"]);
    return (react_1.default.createElement("div", { className: "settings-type-bool" },
        react_1.default.createElement(v_eris_1.Toggle, { inactive: inactive, value: props.value, onChange: props.onChange })));
};
exports.TypeBool = TypeBool;
var TypeCheckbox = function (props) {
    var params = props.params, value = props.value, inactive = props.inactive, rest = tslib_1.__rest(props, ["params", "value", "inactive"]);
    return (react_1.default.createElement("div", { className: "settings-type-checkbox" },
        react_1.default.createElement("input", { type: "checkbox", checked: value ? true : false, onClick: function (e) {
                e.stopPropagation();
            }, onChange: function (e) {
                if (!props.onChange)
                    return;
                props.onChange({ value: e.target.checked });
            } })));
};
exports.TypeCheckbox = TypeCheckbox;
var TypeStacking = function (props, jsx) {
    var stack = props.stack, k = props.k, value = props.value, settings = props.settings, pairs = props.pairs, rest = tslib_1.__rest(props, ["stack", "k", "value", "settings", "pairs"]);
    var refValue = settings ? settings.getValue(stack) : undefined;
    var array = [];
    if (v_eris_1.Common.type(refValue) == "number") {
        refValue = v_eris_1.VMath.clamp(v_eris_1.Common.uint(refValue), 0, 999);
        var override = settings.getOverride(k);
        if (!Array.isArray(value) || !override)
            return array;
        for (var n = 0; n < refValue; n++) {
            var newKey = k + ":" + n;
            array.push(jsx(newKey, n < value.length ? value[n] : (override[0].value[0]), n, undefined));
        }
        ;
        return array;
    }
    else if (v_eris_1.Common.type(refValue) == "array") {
        var override = settings.getOverride(k);
        if (!Array.isArray(value) || !override)
            return array;
        var refLength = refValue.length;
        for (var n = 0; n < refLength; n++) {
            var newKey = k + ":" + n;
            if (pairs) {
                if ((n + 1) < refValue.length) {
                    array.push(jsx(newKey, n < value.length ? value[n] : (override[0].value[0]), n, [refValue[n], refValue[n + 1]]));
                }
                else {
                    break;
                }
                ;
            }
            else {
                array.push(jsx(newKey, n < value.length ? value[n] : (override[0].value[0]), n, refValue[n]));
            }
            ;
        }
        ;
        return array;
    }
    ;
    return [jsx(k, value, undefined)];
};
exports.TypeStacking = TypeStacking;
var TypeNumber = function (props) {
    var params = props.params, value = props.value, stack = props.stack, k = props.k, settings = props.settings, inactive = props.inactive, rest = tslib_1.__rest(props, ["params", "value", "stack", "k", "settings", "inactive"]);
    if (!params)
        params = {};
    return (0, exports.TypeStacking)(props, function (key, value, index, arrayValue) {
        return react_1.default.createElement("div", { key: key, className: "settings-type-number" },
            react_1.default.createElement(v_eris_1.Number, { inactive: inactive, value: value || 0, min: params.min, max: params.max, step: params.step, onChange: function (e) { props.onChange(e, index); } }));
    });
};
exports.TypeNumber = TypeNumber;
var TypeFloat = function (props) {
    var params = props.params, value = props.value, stack = props.stack, k = props.k, settings = props.settings, inactive = props.inactive, rest = tslib_1.__rest(props, ["params", "value", "stack", "k", "settings", "inactive"]);
    if (!params)
        params = {};
    return (0, exports.TypeStacking)(props, function (key, value, index, arrayValue) {
        return react_1.default.createElement("div", { key: key, className: "float input" },
            react_1.default.createElement("input", { type: "number", disabled: inactive, value: value || 0, min: params.min, max: params.max, onChange: function (e) { props.onChange({ value: e.target.value }, index); } }));
    });
};
exports.TypeFloat = TypeFloat;
var TypeRanges = function (props) {
    var params = props.params, value = props.value, stack = props.stack, k = props.k, settings = props.settings, inactive = props.inactive, rest = tslib_1.__rest(props, ["params", "value", "stack", "k", "settings", "inactive"]);
    if (!params)
        params = {};
    return (0, exports.TypeStacking)(props, function (key, value, index, arrayValue) {
        return react_1.default.createElement(types_ranges_1.Ranges, tslib_1.__assign({ key: key, inactive: inactive, value: value }, params, { onChange: function (e) { props.onChange(e, index); } }));
    });
};
exports.TypeRanges = TypeRanges;
var TypeZoom = function (props) {
    var params = props.params, value = props.value, stack = props.stack, k = props.k, settings = props.settings, rest = tslib_1.__rest(props, ["params", "value", "stack", "k", "settings"]);
    if (!params)
        params = {};
    return react_1.default.createElement(v_eris_1.Column, { className: "settings-type-list" }, (0, exports.TypeStacking)(tslib_1.__assign(tslib_1.__assign({}, props), { pairs: true }), function (key, value, index, arrayValue) {
        return react_1.default.createElement(types_zoom_1.ZoomParams, { key: key, value: value || [], range: arrayValue, onChange: function (e) { props.onChange(e, index); } });
    }));
};
exports.TypeZoom = TypeZoom;
var TypeList = function (props) {
    var params = props.params, value = props.value, lang = props.lang, extra = props.extra, settings = props.settings, secure = props.secure, icon = props.icon, rest = tslib_1.__rest(props, ["params", "value", "lang", "extra", "settings", "secure", "icon"]);
    if (!params)
        params = {};
    if (!extra)
        extra = {};
    var secureList = settings && secure ? settings.getSecureValue(secure) : null;
    if (secureList && secureList.list && Array.isArray(secureList.list))
        secureList = secureList.list;
    else
        secureList = null;
    var paramsList = props.params || [];
    if (secureList)
        paramsList = paramsList.filter(function (item) { return secureList.findIndex(function (f) { return f == item.value; }) > -1; });
    return (react_1.default.createElement("div", { className: "settings-type-list" },
        react_1.default.createElement(v_eris_1.MultiSelect, tslib_1.__assign({ value: extra.hasDefault || extra.sortable || (value && value.list) ? ((value ? value.list : value) || []) : (value || []), defaultValue: extra.hasDefault ? (value ? value.defaultValue : null) : (null), sort: extra.sortable || (value && value.sort) ? (value ? value.sort : null) : (null), prefix: extra.prefix, suggestions: paramsList.map(function (item) {
                if (item && (item.value || item.token || item.title))
                    item = { value: item.token || item.value || "", title: item.title || item.name || "", prefix: item.prefix, icon: item.icon, defaultIgnored: item.defaultIgnored };
                else
                    item = { value: item || "", title: item || "" };
                item.title = lang.get(item.title);
                item.prefix = lang.get(item.prefix);
                return { value: item.value, prefix: item.prefix, icon: icon || item.icon, title: item.title, defaultIgnored: item.defaultIgnored };
            }), onChange: function (event) {
                props.onChange(extra.hasDefault || extra.sortable || (value && value.list) ? { value: { list: event.value, defaultValue: event.defaultValue, sort: event.sort } } : { value: event.value });
            } }, extra))));
};
exports.TypeList = TypeList;
var TypeListBinary = function (props) {
    var params = props.params, value = props.value, lang = props.lang, rest = tslib_1.__rest(props, ["params", "value", "lang"]);
    if (!params)
        params = {};
    var val = [];
    value = v_eris_1.Common.uint(value);
    for (var _i = 0, _a = props.params; _i < _a.length; _i++) {
        var item = _a[_i];
        if ((value & item.value) != item.value)
            continue;
        val.push(item.value);
    }
    ;
    return (react_1.default.createElement("div", { className: "settings-type-list" },
        react_1.default.createElement(v_eris_1.MultiSelect, { value: val, suggestions: (props.params || []).map(function (item) {
                if (item && (item.value || item.token || item.title))
                    item = { value: item.token || item.value || "", title: item.title || item.name || "" };
                else
                    item = { value: item || "", title: item || "" };
                item.title = lang.get(item.title);
                return { value: item.value, title: item.title };
            }), onChange: function (e) {
                var result = 0;
                for (var _i = 0, _a = e.value; _i < _a.length; _i++) {
                    var item = _a[_i];
                    result |= item;
                }
                ;
                props.onChange(tslib_1.__assign(tslib_1.__assign({}, e), { value: result }));
            } })));
};
exports.TypeListBinary = TypeListBinary;
var TypeFile = function (props) {
    var multiple = props.params ? props.params.multiple : undefined;
    var ref = (0, react_1.useRef)(null);
    var prev = (0, v_eris_1.usePrevious)(props.value);
    var _a = (0, react_1.useState)([]), searchData = _a[0], setSearchData = _a[1];
    (0, react_1.useEffect)(function () {
        if (!ref.current)
            return;
        if (prev && !props.value) {
            ref.current.value = "";
            list.fetch();
        }
        ;
    }, [props.value]);
    var list = (0, v_eris_1.useAsync)({
        method: "GET",
        url: "./filesystem/search/"
    }, {
        module: props.route,
        field: props.k,
        line: props.token,
        groupBy: 0
    });
    list.onResponse(function (response) {
        if (!response.data || !response.data || !response.data.length)
            return;
        var files = [];
        for (var _i = 0, _a = response.data; _i < _a.length; _i++) {
            var link = _a[_i];
            if (!link.list)
                continue;
            files.push.apply(files, link.list);
        }
        ;
        setSearchData(files);
    });
    (0, react_1.useEffect)(function () {
        list.fetch();
    }, [props.route, props.k, props.token]);
    return (react_1.default.createElement(v_eris_1.Tooltip, { className: "type-file-tooltip", content: react_1.default.createElement("div", null,
            react_1.default.createElement("div", null,
                "Files: ",
                searchData.length),
            react_1.default.createElement("div", null, searchData.map(function (file) {
                return (react_1.default.createElement(v_eris_1.Card, { key: file.token, className: "type-file-tooltip-card" },
                    react_1.default.createElement(v_eris_1.Row, null,
                        "Name: ",
                        file.name),
                    react_1.default.createElement(v_eris_1.Row, null,
                        "Type: ",
                        file.mime),
                    react_1.default.createElement(v_eris_1.Row, null,
                        "Date: ",
                        new Date(v_eris_1.Common.float(file.date) * 1000).toUTCString()),
                    react_1.default.createElement(v_eris_1.Row, null,
                        "User:",
                        react_1.default.createElement(v_eris_1.Space, null),
                        react_1.default.createElement(exports.TypeSelectTableQuery, { inactive: true, lang: props.lang, params: { route: "users", filter: { token: file.user_token } } })),
                    react_1.default.createElement(v_eris_1.Row, null,
                        "User Configuration:",
                        react_1.default.createElement(v_eris_1.Space, null),
                        react_1.default.createElement(exports.TypeSelectTableQuery, { inactive: true, lang: props.lang, params: { route: "configurations", filter: { token: file.user_configuration } } }))));
            }))) },
        react_1.default.createElement("input", { type: "file", ref: ref, multiple: multiple, onChange: function (e) { return props.onChange({ value: e.target.files }); } })));
};
exports.TypeFile = TypeFile;
var TypeNone = function () {
    return react_1.default.createElement("span", null);
};
exports.TypeNone = TypeNone;
exports.Types = {
    enum: react_1.default.createElement(exports.TypeEnum, null),
    theme: react_1.default.createElement(types_theme_1.TypeTheme, null),
    select: react_1.default.createElement(exports.TypeEnum, null),
    text: react_1.default.createElement(exports.TypeText, null),
    textarea: react_1.default.createElement(exports.TypeTextarea, null),
    number: react_1.default.createElement(exports.TypeNumber, null),
    float: react_1.default.createElement(exports.TypeFloat, null),
    ranges: react_1.default.createElement(exports.TypeRanges, null),
    zoom: react_1.default.createElement(exports.TypeZoom, null),
    bool: react_1.default.createElement(exports.TypeBool, null),
    toggle: react_1.default.createElement(exports.TypeBool, null),
    list: react_1.default.createElement(exports.TypeList, null),
    listbinary: react_1.default.createElement(exports.TypeListBinary, null),
    button: react_1.default.createElement(exports.TypeButton, null),
    table: react_1.default.createElement(types_table_1.TypeTable, null),
    checkbox: react_1.default.createElement(exports.TypeCheckbox, null),
    configuration: react_1.default.createElement(exports.TypeSelectConfiguration, null),
    role: react_1.default.createElement(exports.TypeSelectRole, null),
    policy: react_1.default.createElement(exports.TypeSelectPolicy, null),
    querytable: react_1.default.createElement(exports.TypeSelectTableQuery, null),
    json: react_1.default.createElement(exports.TypeJSON, null),
    file: react_1.default.createElement(exports.TypeFile, null),
    none: react_1.default.createElement(exports.TypeNone, null),
};
//# sourceMappingURL=types.js.map