import { pythonGenerator } from "blockly/python";

const Blockly = {
    Python: pythonGenerator || { Msg: Object.create(null) }
};
Blockly.Python.text_join = function (block) {
    // Create a string made up of any number of elements of any type.
    var code;
    if (block.itemCount_ == 0) {
        return ['\'\'', Blockly.Dart.ORDER_ATOMIC];
    } else if (block.itemCount_ == 1) {
        var argument0 = Blockly.Dart.valueToCode(block, 'ADD0',
            Blockly.Dart.ORDER_UNARY_POSTFIX) || '\'\'';
        code = argument0 + '.toString()';
        return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
    } else {
        code = new Array(block.itemCount_);
        for (var n = 0; n < block.itemCount_; n++) {
            code[n] = Blockly.Dart.valueToCode(block, 'ADD' + n,
                Blockly.Dart.ORDER_NONE) || '\'\'';
        }
        code = '[' + code.join(',') + '].join()';
        return [code, Blockly.Dart.ORDER_UNARY_POSTFIX];
    }
};

Blockly.Python['text_split_string_by_delimiter'] = function (block) {
    var value_string_input = Blockly.Python.valueToCode(block, 'string_input', Blockly.Python.ORDER_ATOMIC);
    var text_delimiter = block.getFieldValue('delimiter');
    // TODO: Assemble Python into code variable.
    var code = value_string_input + '.split("' + text_delimiter + '")';
    // TODO: Change ORDER_NONE to the correct strength.
    return [code, Blockly.Python.ORDER_NONE];
};
Blockly.Python.text_format = function (a) {
    var b = Blockly.Python.valueToCode(a, "FORMAT", Blockly.Python.ORDER_ATOMIC);
    let c = Blockly.Python.valueToCode(a, "CONTENT", Blockly.Python.ORDER_ATOMIC);
    return [b + " % " + c, Blockly.Python.ORDER_ATOMIC]
};
Blockly.Python.tuple_create_with_items_insert = function (a) {
    for (var b = Array(a.itemCount_), c = 0; c < a.itemCount_; c++)
        b[c] = Blockly.Python.valueToCode(a, "ADD" + c, Blockly.Python.ORDER_NONE) || "None";
    return [1 == a.itemCount_ ? "(" + b[0] + ",)" : "(" + b.join(", ") + ")", Blockly.Python.ORDER_ATOMIC]
};

Blockly.Python.text_format2 = function (a) {
    var b = Blockly.Python.valueToCode(a, "FORMAT", Blockly.Python.ORDER_ATOMIC);
    a = Blockly.Python.valueToCode(a, "CONTENT", Blockly.Python.ORDER_ATOMIC);
    return [b + ".format" + a, Blockly.Python.ORDER_ATOMIC]
};
Blockly.Python.CocoRobo_text_ESC = function (a) {
    return ['"' + a.getFieldValue("mode") + '"', Blockly.Python.ORDER_ATOMIC]
};
Blockly.Python.text_append_text = function (a) {
    var b = Blockly.Python.valueToCode(a, "text_abc", Blockly.Python.ORDER_ATOMIC);
    a = Blockly.Python.valueToCode(a, "append_text", Blockly.Python.ORDER_ATOMIC);
    return ["str(" + b + ") + str(" + a + ")", Blockly.Python.ORDER_ATOMIC]
};
Blockly.Python.text_is_number = function (a) {
    var b = Blockly.Python.valueToCode(a, "TEXT", Blockly.Python.ORDER_ATOMIC);
    a = a.getFieldValue("TYPE");
    return [b + a, Blockly.Python.ORDER_ATOMIC]
};
Blockly.Python.text_to_byte = function (a) {
    return ["bytes(" + Blockly.Python.valueToCode(a, "TEXT", Blockly.Python.ORDER_ATOMIC) + ", 'utf-8')", Blockly.Python.ORDER_ATOMIC]
};
Blockly.Python.other_to_byte = function (a) {
    return ["bytes(" + Blockly.Python.valueToCode(a, "input_var", Blockly.Python.ORDER_ATOMIC) + ")", Blockly.Python.ORDER_ATOMIC]
};
Blockly.Python.bit_inversion = function (a) {
    return ["~" + Blockly.Python.valueToCode(a, "data", Blockly.Python.ORDER_ATOMIC), Blockly.Python.ORDER_ATOMIC]
};
Blockly.Python.CocoRobo_bytes_decode = function (a) {
    return [Blockly.Python.valueToCode(a, "bytes_decode", Blockly.Python.ORDER_ATOMIC) + ".decode('UTF-8','ignore')", Blockly.Python.ORDER_ATOMIC]
};
Blockly.Python.CocoRobo_ujson_dumps = function (a) {
    Blockly.Python.definitions_.import_ujson = "import json";
    return ["json.dumps(" + Blockly.Python.valueToCode(a, "data", Blockly.Python.ORDER_ATOMIC) + ")", Blockly.Python.ORDER_ATOMIC]
};
Blockly.Python.CocoRobo_ujson_loads = function (a) {
    Blockly.Python.definitions_.import_ujson = "import json";
    return ["json.loads(" + Blockly.Python.valueToCode(a, "data", Blockly.Python.ORDER_ATOMIC) + ")", Blockly.Python.ORDER_ATOMIC]
};
Blockly.Python.CocoRobo_code_annotation = function (a) {
    var code = Blockly.Python.valueToCode(a, "data", Blockly.Python.ORDER_ATOMIC);
    return "# " + code.replace("'", '').replace("'", '') + "\n";
};
Blockly.Python.text_dict = function (a) {
    return ["{" + Blockly.Python.quote_empty(a.getFieldValue("TEXT")) + "}", Blockly.Python.ORDER_ATOMIC]
};

export default Blockly;