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;
};
import { JsonDeserialize, JsonSerialize, JsonNaming, JsonProperty, JsonClassType, PropertyNamingStrategy } from 'jackson-js';
import { v4 as uuid } from 'uuid';
import { DateSerializer } from '../jack-x/date';
import { MessageType } from './constants';
export class TMNSerializer {
    static serialize(tmn) {
        return tmn.Tmn;
    }
    static deserialize(o) {
        return new TMN(o);
    }
}
export class TMN {
    constructor(tmn) {
        this.Tmn = tmn;
    }
    static NewCommandTMN(target, method) {
        if (!TMN.checkTokens(target) || !TMN.checkTokens(method)) {
            throw new URIError(`invalid characters in tmn components NewCommandTMN ${target} ${method}`);
        }
        return new TMN(`${MessageType.MESSAGE_TYPE_COMMAND}:${target}:${method}`);
    }
    static NewEventTMN(namespace, resourceType, resourceSubtype) {
        if (!TMN.checkTokens(namespace) || !TMN.checkTokens(resourceType)) {
            throw new URIError(`invalid characters in tmn components NewEventTMN ${namespace} ${resourceType}`);
        }
        if (resourceSubtype && !TMN.checkTokens(resourceSubtype)) {
            throw new URIError(`invalid characters in tmn resourceSubtype NewEventTMN ${resourceSubtype}`);
        }
        if (resourceSubtype) {
            return new TMN(`${MessageType.MESSAGE_TYPE_EVENT}:${namespace}:${resourceType}:${resourceSubtype}`);
        }
        return new TMN(`${MessageType.MESSAGE_TYPE_EVENT}:${namespace}:${resourceType}`);
    }
    static checkTokens(str) {
        let code, i, len;
        for (i = 0, len = str.length; i < len; i++) {
            code = str.charCodeAt(i);
            if (!(code > 47 && code < 58) && // numeric (0-9)
                code != 45 && // -
                !(code > 96 && code < 123)) { // lower alpha (a-z)
                return false;
            }
        }
        return true;
    }
    toString() {
        return this.Tmn;
    }
    getType() {
        if (this.Tmn.startsWith(MessageType.MESSAGE_TYPE_COMMAND)) {
            return MessageType.MESSAGE_TYPE_COMMAND;
        }
        if (this.Tmn.startsWith(MessageType.MESSAGE_TYPE_EVENT)) {
            return MessageType.MESSAGE_TYPE_EVENT;
        }
        throw new TypeError("invalid tmn type");
    }
    tokenize() {
        return this.Tmn.split(":");
    }
    getNamespace() {
        if (this.getType() != MessageType.MESSAGE_TYPE_EVENT) {
            return undefined;
        }
        return this.tokenize()[1];
    }
    getResourceType() {
        if (this.getType() != MessageType.MESSAGE_TYPE_EVENT) {
            return undefined;
        }
        return this.tokenize()[2];
    }
    getResourceSubtype() {
        if (this.getType() != MessageType.MESSAGE_TYPE_EVENT) {
            return undefined;
        }
        const tokens = this.tokenize();
        if (tokens.length < 4) {
            return undefined;
        }
        return this.tokenize()[3];
    }
    getTarget() {
        if (this.getType() != MessageType.MESSAGE_TYPE_COMMAND) {
            return undefined;
        }
        return this.tokenize()[1];
    }
    getMethod() {
        if (this.getType() != MessageType.MESSAGE_TYPE_COMMAND) {
            return undefined;
        }
        return this.tokenize()[2];
    }
}
__decorate([
    JsonProperty()
], TMN.prototype, "Tmn", void 0);
export class TRNSerializer {
    static serialize(trn) {
        return trn.Trn;
    }
    static deserialize(o) {
        return new TRN(o);
    }
}
export class TRN {
    constructor(tmn) {
        this.Trn = tmn;
    }
    static NewTRN(parent, namespace, resourceType, id) {
        if (!TRN.checkTokens(parent) ||
            !TRN.checkTokens(namespace) ||
            !TRN.checkTokens(resourceType)) {
            throw new URIError(`invalid characters in trn components NewTRN ${parent} ${namespace} ${resourceType} ${id}`);
        }
        return new TRN(`parents/${parent}/namespaces/${namespace}/${resourceType}/${id}`);
    }
    static checkTokens(str) {
        let code, i, len;
        for (i = 0, len = str.length; i < len; i++) {
            code = str.charCodeAt(i);
            if (!(code > 47 && code < 58) && // numeric (0-9)
                code != 45 && code != 46 && // - .
                !((code > 96 && code < 123) || (code > 64 && code < 91))) { // lower alpha (a-z) or upper alpha (A-Z)
                return false;
            }
        }
        return true;
    }
    toString() {
        return this.Trn;
    }
    tokenize() {
        return this.Trn.split("/");
    }
    getParent() {
        return this.tokenize()[1];
    }
    getNamespace() {
        return this.tokenize()[3];
    }
    getResourceType() {
        return this.tokenize()[4];
    }
    getResourceId() {
        // get all token after 5th and join with / to allow any character as resource ID
        return this.tokenize().slice(5).join("/");
    }
    getResourceName() {
        return `${this.getResourceType()}/${this.getResourceId()}`;
    }
}
__decorate([
    JsonProperty()
], TRN.prototype, "Trn", void 0);
let Message = class Message {
    constructor(tmn, ServiceTrn, config = undefined) {
        this.Tmn = tmn;
        this.Timestamp = new Date();
        this.ServiceTrn = ServiceTrn;
        this.MessageID = uuid();
        this.ParentID = (config === null || config === void 0 ? void 0 : config.ParentID) != undefined ? config === null || config === void 0 ? void 0 : config.ParentID : "";
        this.GroupId = (config === null || config === void 0 ? void 0 : config.GroupId) != undefined ? config === null || config === void 0 ? void 0 : config.GroupId : uuid();
    }
};
__decorate([
    JsonProperty(),
    JsonSerialize({ using: TMNSerializer.serialize }),
    JsonDeserialize({ using: TMNSerializer.deserialize }),
    JsonClassType({ type: () => [TMN] })
], Message.prototype, "Tmn", void 0);
__decorate([
    JsonProperty(),
    JsonSerialize({ using: DateSerializer.serialize }),
    JsonDeserialize({ using: DateSerializer.deserialize }),
    JsonClassType({ type: () => [Date] })
], Message.prototype, "Timestamp", void 0);
__decorate([
    JsonProperty(),
    JsonClassType({ type: () => [String] })
], Message.prototype, "MessageID", void 0);
__decorate([
    JsonProperty(),
    JsonClassType({ type: () => [String] })
], Message.prototype, "ParentID", void 0);
__decorate([
    JsonProperty(),
    JsonClassType({ type: () => [String] })
], Message.prototype, "GroupId", void 0);
__decorate([
    JsonProperty(),
    JsonSerialize({ using: TRNSerializer.serialize }),
    JsonDeserialize({ using: TRNSerializer.deserialize }),
    JsonClassType({ type: () => [TRN] })
], Message.prototype, "ServiceTrn", void 0);
Message = __decorate([
    JsonNaming({ strategy: PropertyNamingStrategy.KEBAB_CASE })
], Message);
export { Message };
//# sourceMappingURL=message.js.map