import ProtocolEnum from '../enum/ProtocolEnum.js';
/**
 *
 * The Resource Locator is a way for the nanotdf to represent references to external resources in as succinct a format
 * as possible.
 *
 * | Section       | Minimum Length (B) | Maximum Length (B) |
 * |---------------|--------------------|--------------------|
 * | Protocol Enum | 1                  | 1                  |
 * | Body Length   | 1                  | 1                  |
 * | Body          | 1                  | 255                |
 *
 * @link https://github.com/virtru/nanotdf/blob/master/spec/index.md#3312-kas
 * @link https://github.com/virtru/nanotdf/blob/master/spec/index.md#341-resource-locator
 */
export default class ResourceLocator {
    static parse(url) {
        const [protocol, body] = url.split('://');
        // Buffer to hold the protocol, length of body, body
        const buffer = new Uint8Array(1 + 1 + body.length);
        buffer.set([body.length], 1);
        buffer.set(new TextEncoder().encode(body), 2);
        if (protocol.toLowerCase() == 'http') {
            buffer.set([ProtocolEnum.Http], 0);
        }
        else if (protocol.toLowerCase() == 'https') {
            buffer.set([ProtocolEnum.Https], 0);
        }
        else {
            throw new Error('Resource locator protocol is not supported.');
        }
        return new ResourceLocator(buffer);
    }
    constructor(buff) {
        this.offset = 0;
        // Protocol
        this.protocol = buff[ResourceLocator.PROTOCOL_OFFSET];
        // Length of body
        this.lengthOfBody = buff[ResourceLocator.LENGTH_OFFSET];
        // Body as utf8 string
        const decoder = new TextDecoder();
        this.body = decoder.decode(buff.subarray(ResourceLocator.BODY_OFFSET, ResourceLocator.BODY_OFFSET + this.lengthOfBody));
        this.offset =
            ResourceLocator.PROTOCOL_LENGTH + ResourceLocator.LENGTH_LENGTH + this.lengthOfBody;
    }
    /**
     * Length
     *
     * @returns { number } Length of resource locator
     */
    get length() {
        return (
        // Protocol
        1 +
            // Length of the body( 1 byte)
            1 +
            // Content length
            this.body.length);
    }
    get url() {
        switch (this.protocol) {
            case ProtocolEnum.Http:
                return 'http://' + this.body;
            case ProtocolEnum.Https:
                return 'https://' + this.body;
            default:
                throw new Error('Resource locator protocol is not supported.');
        }
    }
    /**
     * Return the contents of the Resource Locator in buffer
     */
    toBuffer() {
        const buffer = new Uint8Array(2 + this.body.length);
        buffer.set([this.protocol], 0);
        buffer.set([this.lengthOfBody], 1);
        buffer.set(new TextEncoder().encode(this.body), 2);
        return buffer;
    }
    /**
     * Get URL
     *
     * Construct URL from ResourceLocator or throw error
     */
    getUrl() {
        let protocol;
        if (this.protocol === ProtocolEnum.Http) {
            protocol = 'http';
        }
        else if (this.protocol === ProtocolEnum.Https) {
            protocol = 'https';
        }
        else {
            throw new Error(`Cannot create URL from protocol, ${ProtocolEnum[this.protocol]}`);
        }
        return `${protocol}://${this.body}`;
    }
}
ResourceLocator.PROTOCOL_OFFSET = 0;
ResourceLocator.PROTOCOL_LENGTH = 1;
ResourceLocator.LENGTH_OFFSET = 1;
ResourceLocator.LENGTH_LENGTH = 1;
ResourceLocator.BODY_OFFSET = 2;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiUmVzb3VyY2VMb2NhdG9yLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vLi4vc3JjL25hbm90ZGYvbW9kZWxzL1Jlc291cmNlTG9jYXRvci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLFlBQVksTUFBTSxzQkFBc0IsQ0FBQztBQUVoRDs7Ozs7Ozs7Ozs7OztHQWFHO0FBQ0gsTUFBTSxDQUFDLE9BQU8sT0FBTyxlQUFlO0lBWWxDLE1BQU0sQ0FBQyxLQUFLLENBQUMsR0FBVztRQUN0QixNQUFNLENBQUMsUUFBUSxFQUFFLElBQUksQ0FBQyxHQUFHLEdBQUcsQ0FBQyxLQUFLLENBQUMsS0FBSyxDQUFDLENBQUM7UUFFMUMsb0RBQW9EO1FBQ3BELE1BQU0sTUFBTSxHQUFHLElBQUksVUFBVSxDQUFDLENBQUMsR0FBRyxDQUFDLEdBQUcsSUFBSSxDQUFDLE1BQU0sQ0FBQyxDQUFDO1FBQ25ELE1BQU0sQ0FBQyxHQU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