var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
import { Injectable, Inject } from '@angular/core';
import { LOCALIZATION } from '../../localization';
let FBoxDataTypeService = class FBoxDataTypeService {
    constructor(localization) {
        this.localization = localization;
        this.defaultIntegerDigits = 4;
        this.defaultFractionDigits = 0;
        // 0, 'Bit'
        // 1, 'UInt16'
        // 2, 'Int16'
        // 3, 'Bcd16'
        // 4, 'Hex16'
        // 5, 'Binary16'
        // 11, 'UInt32'
        // 12, 'Int32'
        // 13, 'Bcd32'
        // 14, 'Hex32'
        // 15, 'Binary32'
        // 16, 'Single'
        // 30, 'String'
    }
    getValueType(id) {
        switch (id) {
            case 0:
            case 5:
            case 15:
                return this.localization.binaryType;
            case 1:
            case 2:
            case 3:
            case 11:
            case 12:
            case 13:
            case 16:
                return this.localization.decimalType;
            case 4:
            case 14:
                return this.localization.hexadecimalType;
            case 30:
                return this.localization.stringType;
        }
    }
    getValuePattern(id) {
        switch (id) {
            case 0:
            case 5:
            case 15:
                return '^[01]+$';
            case 1:
            case 2:
            case 3:
            case 11:
            case 12:
            case 13:
            case 16:
                return '^[+-]?[0-9]*(\\.[0-9]*)?$';
            case 4:
            case 14:
                return '^[0-9a-fA-F]+$';
        }
    }
    isHexType(id) {
        return id === 4 || id === 14;
    }
    isBinaryType(id) {
        return id === 5 || id === 15;
    }
    isHexOrBinaryType(id) {
        return id === 4 || id === 14 || id === 5 || id === 15;
    }
    isFloat(id) {
        return id === 16;
    }
    /**
     * 获取数据型号最大长度
     * @param id
     */
    getValueMaxLength(id) {
        switch (id) {
            case 1: // UInt16
                return 5;
            case 2: // Int16
                return 6;
            case 3: // Bcd16
            case 4: // Hex16
                return 4;
            case 5: // Binary16
                return 16;
            case 11: // UInt32
                return 10;
            case 12: // Int32
            case 16: // Single
                return 11;
            case 13: // Bcd32
                return 8;
            case 14: // Hex32
                return 8;
            case 15: // Binary32
                return 32;
            case 21: // UInt64
                return 16;
            case 22: // Int64
                return 16;
            case 23: // Bcd64
                return 16;
            case 24: // Hex64
                return 8;
            case 25: // Binary64
                return 32;
            default:
                return 0;
        }
    }
    /**
     * 是否支持小数位
     * @param id
     */
    isSupportFractionDigit(id) {
        return id !== 0 && id !== 4 && id !== 5 && id !== 14 && id !== 15 && id !== 30;
    }
    /**
     * 是否为有符号数
     * @param id
     */
    isSignedType(id) {
        return id === 2 || id === 12 || id === 16;
    }
    /**
     * 是否支持数值运算
     * @param id
     */
    isSupportNumericalOperation(id) {
        return id !== 0 && id !== 4 && id !== 5 && id !== 14 && id !== 15 && id !== 30;
    }
    /**
    * 数据转换成10进制
    * @param value
    * @param id
    */
    formatToDecimal(value, id) {
        if (this.isBinaryType(id)) {
            return parseInt(value, 2);
        }
        if (this.isHexType(id)) {
            return parseInt(value, 16);
        }
        return value;
    }
    /**
       * 获取数据型号最小值
       * @param id
       * @param? integerDigits
       * @param? fractionDigits
       */
    getMinValue(id, integerDigits, fractionDigits) {
        let minValue;
        switch (id) {
            case 1: // UInt16
            case 3: // Bcd16
            case 4: // Hex16
            case 5: // Binary16
            case 11: // UInt32
            case 13: // Bcd32
            case 14: // Hex32
            case 15: // Binary32
            case 21: // UInt64
            case 23: // Bcd64
            case 14: // Hex32
            case 15: // Binary32
            case 24: // Hex64
            case 25: // Binary64
                minValue = 0;
                break;
            case 2: // Int16
                minValue = -32768;
                break;
            case 12: // Int32
                minValue = -2147483648;
                break;
            case 16: // Single
                minValue = -1000000000;
                break;
            case 22: // Int64
                // minValue = -9223372036854775808;
                minValue = Number.MIN_SAFE_INTEGER;
                break;
            case 26: // Double
                minValue = -Number.MAX_VALUE;
                break;
        }
        if (integerDigits) {
            let digitsLength = integerDigits;
            if (fractionDigits) {
                digitsLength = digitsLength + fractionDigits;
            }
            if (digitsLength) {
                if (minValue.toString().length - 1 > digitsLength) {
                    minValue = -this.getFillNine(digitsLength);
                }
            }
            if (this.isFloat(id) && fractionDigits) {
                minValue = minValue / Math.pow(10, fractionDigits);
            }
        }
        return minValue;
    }
    /**
       * 获取数据型号最大值
       * @param id
       * @param? integerDigits
       * @param? fractionDigits
       */
    getMaxValue(id, integerDigits, fractionDigits) {
        let maxValue;
        switch (id) {
            case 1: // UInt16
            case 4: // Hex16
            case 5: // Binary16
                maxValue = 65535;
                break;
            case 2: // Int16
                maxValue = 32767;
                break;
            case 3: // Bcd16
                maxValue = 9999;
                break;
            case 11: // UInt32
            case 14: // Hex32
            case 15: // Binary32
                maxValue = 4294967295;
                break;
            case 12: // Int32
                maxValue = 2147483647;
                break;
            case 13: // Bcd32
                maxValue = 99999999;
                break;
            case 16: // Single
                maxValue = 1000000000;
                break;
            case 21: // UInt64
            case 24: // Hex64
            case 25: // Binary64
                // maxValue = 18446744073709551615;
                maxValue = Number.MAX_SAFE_INTEGER;
                break;
            case 22: // Int64
                // maxValue = 9223372036854775807;
                maxValue = Number.MAX_SAFE_INTEGER;
                break;
            case 23: // Bcd64
                // maxValue = 9999999999999999;
                maxValue = Number.MAX_SAFE_INTEGER;
                break;
            case 26: // Double
                maxValue = Number.MAX_VALUE;
                break;
        }
        if (integerDigits) {
            let digitsLength = integerDigits;
            if (fractionDigits) {
                digitsLength = digitsLength + fractionDigits;
            }
            if (digitsLength) {
                if (maxValue.toString().length > digitsLength) {
                    maxValue = this.getFillNine(digitsLength);
                }
            }
            if (this.isFloat(id) && fractionDigits) {
                maxValue = maxValue / Math.pow(10, fractionDigits);
            }
        }
        return maxValue;
    }
    getFillNine(num) {
        let text = '9';
        for (let i = 1; i < num; i++) {
            text = `9${text}`;
        }
        return text;
    }
};
FBoxDataTypeService = __decorate([
    Injectable(),
    __param(0, Inject(LOCALIZATION)),
    __metadata("design:paramtypes", [Object])
], FBoxDataTypeService);
export { FBoxDataTypeService };
