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';
import { FCloudDataType } from '../../model/shared/data-type/fcloud-data-Type';
let FCloudDataTypeService = class FCloudDataTypeService {
    constructor(localization) {
        this.localization = localization;
        this.defaultIntegerDigits = 4;
        this.defaultFractionDigits = 0;
        this.dataTypeList = new Map();
        // 1, 'Int32'
        // 2, 'Float'
        // 4, 'Bool'
        // 8, 'String'
        // 90, 'Binary'
        // 99, 'Hex'
    }
    getValueType(id) {
        switch (id) {
            case FCloudDataType.Binary:
                return this.localization.binaryType;
            case FCloudDataType.Int32:
            case FCloudDataType.Float:
                return this.localization.decimalType;
            case FCloudDataType.Hex:
                return this.localization.hexadecimalType;
            case FCloudDataType.String:
                return this.localization.stringType;
        }
    }
    getValuePattern(id) {
        switch (id) {
            case FCloudDataType.Binary:
                return '^[01]+$';
            case FCloudDataType.Int32:
            case FCloudDataType.Float:
                return '^[+-]?[0-9]*(\\.[0-9]*)?$';
            case FCloudDataType.Hex:
                return '^[0-9a-fA-F]+$';
        }
    }
    isHexType(id) {
        return id === FCloudDataType.Hex;
    }
    isBinaryType(id) {
        return id === FCloudDataType.Binary;
    }
    isHexOrBinaryType(id) {
        return id === FCloudDataType.Binary || id === FCloudDataType.Hex;
    }
    isFloat(id) {
        return id === FCloudDataType.Float;
    }
    /**
     * 获取数据型号最大长度
     * @param id
     */
    getValueMaxLength(id) {
        switch (id) {
            case FCloudDataType.Int32: // Int32
                return 10;
            case FCloudDataType.Float:
                return 11;
            case FCloudDataType.Binary: // Binary
                return 32;
            case FCloudDataType.Hex: // Hex
                return 8;
            default:
                return 0;
        }
    }
    /**
     * 是否支持小数位
     * @param id
     */
    isSupportFractionDigit(id) {
        return id !== FCloudDataType.Bool && id !== FCloudDataType.String && id !== FCloudDataType.Binary && id !== FCloudDataType.Hex;
    }
    /**
    * 是否为有符号数
    * @param id
    */
    isSignedType(id) {
        return id === FCloudDataType.Int32 || id === FCloudDataType.Float;
    }
    /**
     * 是否支持数值运算
     * @param id
     */
    isSupportNumericalOperation(id) {
        return id !== FCloudDataType.Bool && id !== FCloudDataType.Binary && id !== FCloudDataType.Hex && id !== FCloudDataType.String;
    }
    /**
    * 数据转换成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 FCloudDataType.Binary:
            case FCloudDataType.Hex:
                minValue = 0;
                break;
            case FCloudDataType.Int32:
                minValue = -2147483648;
                break;
            case FCloudDataType.Float:
                minValue = -1000000000;
                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 FCloudDataType.Binary:
            case FCloudDataType.Hex:
                maxValue = 4294967295;
                break;
            case FCloudDataType.Int32:
                maxValue = 2147483647;
                break;
            case FCloudDataType.Float:
                maxValue = 1000000000;
                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;
    }
};
FCloudDataTypeService = __decorate([
    Injectable(),
    __param(0, Inject(LOCALIZATION)),
    __metadata("design:paramtypes", [Object])
], FCloudDataTypeService);
export { FCloudDataTypeService };
