/* eslint-disable no-unused-expressions */
import * as Blockly from 'blockly';
import CategoryColors from './define_color'

// Blockly.FieldDropdown.prototype.setText = function (a) {
//     this.sourceBlock_ && this.arrow_ && (this.arrow_.style.fill = this.sourceBlock_.getColour());
//     null !== a && a !== this.text_ && (this.text_ = a,
//         this.updateTextNode_(),
//         this.textElement_ && (this.sourceBlock_.RTL ? this.textElement_.insertBefore(this.arrow_, this.textElement_.firstChild) : this.textElement_.appendChild(this.arrow_)),
//         this.sourceBlock_ && this.sourceBlock_.rendered && (this.sourceBlock_.render(),
//             this.sourceBlock_.bumpNeighbours_()))
// };
// Blockly.FieldDropdown.prototype.setValue = function (a) {
//     if (null !== a && a !== this.value_) {
//         this.sourceBlock_ && Blockly.Events.isEnabled() && Blockly.Events.fire(new Blockly.Events.Change(this.sourceBlock_, "field", this.name, this.value_, a));
//         this.value_ = a;
//         for (var b = this.getOptions_(), c = 0; c < b.length; c++)
//             if (b[c][1] == a) {
//                 this.setText(b[c][0]);
//                 return
//             }
//         this.setText(a)
//     }
// };
// Blockly.FieldDropdown.prototype.getOptions_ = function () {
//     return Blockly.isFunction(this.menuGenerator_) ? this.menuGenerator_.call(this) : this.menuGenerator_
// };
// Blockly.isFunction = function (a) {
//     return "function" == Blockly.typeOf(a)
// };
Blockly.typeOf = function (a) {
    var b = typeof a;
    if ("object" == b)
        if (a) {
            if (a instanceof Array)
                return "array";
            if (a instanceof Object)
                return b;
            var c = Object.prototype.toString.call(a);
            if ("[object Window]" == c)
                return "object";
            if ("[object Array]" == c || "number" == typeof a.length && "undefined" != typeof a.splice && "undefined" != typeof a.propertyIsEnumerable && !a.propertyIsEnumerable("splice"))
                return "array";
            if ("[object Function]" == c || "undefined" != typeof a.call && "undefined" != typeof a.propertyIsEnumerable && !a.propertyIsEnumerable("call"))
                return "function"
        } else
            return "null";
    else if ("function" == b && "undefined" == typeof a.call)
        return "object";
    return b
}
Blockly.Blocks.dict_create_with_items_insert = {
    init: function () {
        this.setColour(CategoryColors.Dictionary);
        this.appendDummyInput("")
            .appendField(new Blockly.FieldLabel(Blockly.Msg.DICT_CREATE_WITH_INPUT_WITH), "TIP");
        this.itemCount_ = 3;
        // this.updateShape_();
        this.setOutput(!0);
        this.setMutator(new Blockly.icons.MutatorIcon(["dict_create_with_item"], this));
        this.setTooltip(Blockly.Msg.DICT_CREATE_WITH_INPUT_WITH)
    },
    mutationToDom: function () {
        var a = document.createElement("mutation");
        a.setAttribute("items", this.itemCount_);
        return a
    },
    domToMutation: function (a) {
        this.itemCount_ = parseInt(a.getAttribute("items"), 10);
        this.updateShape_()
    },
    decompose: function (a) {
        var b = a.newBlock("dict_create_with_container");
        b.initSvg();
        for (var c = b.getInput("STACK").connection, e = 0; e < this.itemCount_; e++) {
            var d = a.newBlock("dict_create_with_item");
            d.initSvg();
            c.connect(d.previousConnection);
            c = d.nextConnection
        }
        return b
    },
    compose: function (a) {
        a = a.getInputTargetBlock("STACK");
        for (var b = [], c = 0; a;)
            b[c] = a.valueConnection_,
                a = a.nextConnection && a.nextConnection.targetBlock(),
                c++;
        this.itemCount_ = c;
        this.updateShape_();
        for (c = 0; c < this.itemCount_; c++)
            b[c] && this.getInput("ADD" + c).connection.connect(b[c])
    },
    saveConnections: function (a) {
        a = a.getInputTargetBlock("STACK");
        for (var b = 0; a;) {
            var c = this.getInput("ADD" + b);
            a.valueConnection_ = c && c.connection.targetConnection;
            b++;
            a = a.nextConnection && a.nextConnection.targetBlock()
        }
    },
    updateShape_: function () {
        this.getInput("EMPTY") && this.removeInput("EMPTY");
        for (var a = [], b = 0; this.getInput("ADD" + b); b++)
            a.push(this.getFieldValue("KEY" + b)),
                this.removeInput("ADD" + b);
        if (0 === this.itemCount_)
            this.getField("TIP").setText(Blockly.Msg.DICT_CREATE_EMPTY_TITLE);
        else
            for (this.getField("TIP").setText(Blockly.Msg.DICT_CREATE_WITH_INPUT_WITH),
                b = 0; b < this.itemCount_; b++)
                this.appendValueInput("ADD" + b).setCheck(null).setAlign(Blockly.ALIGN_RIGHT).appendField(new Blockly.FieldTextInput(a.length > b ? a[b] : 0 == b ? "key_0" : "key_" + b), "KEY" + b).appendField(":")
    },
    getVars: function () {
        return [this.getFieldValue("VAR")]
    },
    renameVar: function (a, b) {
        Blockly.Names.equals(a, this.getFieldValue("VAR")) && this.setTitleValue(b, "VAR")
    }
};
Blockly.Blocks.dict_create_with_item = {
    init: function () {
        this.setColour(CategoryColors.Dictionary);
        this.appendDummyInput().appendField(Blockly.Msg.VARIABLES_DEFAULT_NAME);
        this.setPreviousStatement(!0);
        this.setNextStatement(!0);
        this.setTooltip(Blockly.Msg.DICT_CREATE_WITH_ITEM_TOOLTIP);
        this.contextMenu = !1
    }
};
Blockly.Blocks.dict_create_with_container = {
    init: function () {
        this.setColour(CategoryColors.Dictionary);
        this.appendDummyInput().appendField(Blockly.Msg.DICT_CREATE_WITH_CONTAINER_TITLE_ADD);
        this.appendStatementInput("STACK");
        this.setTooltip(Blockly.Msg.DICT_CREATE_WITH_CONTAINER_TOOLTIP);
        this.contextMenu = !1
    }
};
Blockly.Blocks.dict_create_with = {
    init: function () {
        this.jsonInit({
            inputsInline: !0,
            nextStatement: null,
            previousStatement: null,
            colour: CategoryColors.Dictionary,
            helpUrl: Blockly.Msg.DICT_CREATE_WITH_HELPURL,
            tooltip: Blockly.Msg.DICT_CREATE_WITH_TOOLTIP,
            message0: Blockly.Msg.DICT_CREATE_WITH_MESSAGE0,
            args0: [{
                type: "input_value",
                name: "dict_name"
            }, {
                type: "input_value",
                name: "dict_items"
            }]
        })
    }
};
Blockly.Blocks.dict_key_value = {
    init: function () {
        this.jsonInit({
            colour: CategoryColors.Dictionary,
            args0: [{
                type: "input_value",
                name: "dict_name"
            }, {
                type: "input_value",
                name: "dict_items"
            }],
            output: ["String", "Number", "Boolean", "Array"],
            inputsInline: !0,
            helpUrl: Blockly.Msg.DICT_KEY_VALUE_HELPURL,
            tooltip: Blockly.Msg.DICT_KEY_VALUE_TOOLTIP,
            message0: Blockly.Msg.DICT_KEY_VALUE_MESSAGE0
        })
    }
};
Blockly.Blocks.add_dict_key_value = {
    init: function () {
        this.jsonInit({
            colour: CategoryColors.Dictionary,
            args0: [{
                type: "input_value",
                name: "dict_name"
            }, {
                type: "input_value",
                name: "dict_items"
            }, {
                type: "input_value",
                name: "add_value"
            }],
            inputsInline: !0,
            nextStatement: null,
            previousStatement: null,
            helpUrl: Blockly.Msg.ADD_DICT_KEY_VALUE_HELPURL,
            tooltip: Blockly.Msg.ADD_DICT_KEY_VALUE_TOOLTIP,
            message0: Blockly.Msg.ADD_DICT_KEY_VALUE_MESSAGE0
        })
    }
};
Blockly.Blocks.dict_length = {
    init: function () {
        this.jsonInit({
            colour: CategoryColors.Dictionary,
            args0: [{
                type: "input_value",
                name: "dict_name"
            }],
            output: "Number",
            inputsInline: !0,
            helpUrl: Blockly.Msg.DICT_LENGTH_HELPURL,
            tooltip: Blockly.Msg.DICT_LENGTH_TOOLTIP,
            message0: Blockly.Msg.DICT_LENGTH_MESSAGE0
        })
    }
};
Blockly.Blocks.dict_key_exist = {
    init: function () {
        this.jsonInit({
            colour: CategoryColors.Dictionary,
            args0: [{
                type: "input_value",
                name: "dict_name"
            }, {
                type: "input_value",
                name: "dict_items"
            }],
            output: "Boolean",
            inputsInline: !0,
            helpUrl: Blockly.Msg.DICT_KEY_EXIST_HELPURL,
            tooltip: Blockly.Msg.DICT_KEY_EXIST_TOOLTIP,
            message0: Blockly.Msg.DICT_KEY_EXIST_MESSAGE0
        })
    }
};
Blockly.Blocks.dict_key_or_val_list = {
    init: function () {
        this.jsonInit({
            colour: CategoryColors.Dictionary,
            args0: [{
                type: "input_value",
                name: "dict_name"
            }, {
                options: [
                    [Blockly.Msg.DICT_KEYS, "keys"],
                    [Blockly.Msg.DICT_VALUES, "values"]
                ],
                type: "field_dropdown",
                name: "key_or_val"
            }],
            output: "Array",
            inputsInline: !0,
            helpUrl: Blockly.Msg.DICT_KEY_OR_VAL_LIST_HELPURL,
            // tooltip: Blockly.Msg.DICT_KEY_OR_VAL_LIST_TOOLTIP,
            message0: Blockly.Msg.DICT_KEY_OR_VAL_LIST_MESSAGE0
        });
        var thisBlock = this;
        this.setTooltip(function () {
            var mode = thisBlock.getFieldValue('key_or_val');
            var TOOLTIPS = {
                'keys': Blockly.Msg.DICT_KEY_OR_VAL_LIST_TOOLTIP.replace('%2', Blockly.Msg.DICT_KEYS),
                'values': Blockly.Msg.DICT_KEY_OR_VAL_LIST_TOOLTIP.replace('%2', Blockly.Msg.DICT_VALUES)
            };
            return TOOLTIPS[mode];
        });
    }
};


export default Blockly;