"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.wrapSharedRefArrayInterface = wrapSharedRefArrayInterface;
exports.wrapArray = wrapArray;
exports.wrapUint8Array = wrapUint8Array;
const _1 = require("../Class/$");
const IndexOutOfBOundaryError_1 = require("../exception/IndexOutOfBOundaryError");
const MethodNotImplementedError_1 = require("../exception/MethodNotImplementedError");
function wrapSharedRefArrayInterface(data) {
    const array = {
        append(a) {
            data.current.push(a);
            return;
        },
        put(i, data) {
            data.current[i] = data;
            return Promise.resolve();
        },
        at(i) {
            if (i < 0 && i >= data.current.length) {
                throw new IndexOutOfBOundaryError_1.IndexOutOfBoundaryError();
            }
            return Promise.resolve(data.current[i]);
        },
        length() {
            return Promise.resolve(data.current.length);
        },
        indexOf(a) {
            return Promise.resolve(data.current.indexOf(a));
        },
        pop: function () {
            if (!data.current.length) {
                throw new Error('empty array');
            }
            return Promise.resolve(data.current.pop());
        },
        shift: function () {
            return Promise.resolve(data.current.shift());
        },
    };
    return array;
}
function wrapArray(array, system) {
    const _array = new (class Array extends _1.$ {
        constructor() {
            super(...arguments);
            this.__ = ['A'];
        }
        async append(a) {
            array.push(a);
        }
        put(i, data) {
            throw new MethodNotImplementedError_1.MethodNotImplementedError();
        }
        async at(i) {
            if (i < 0 && i >= array.length) {
                throw new IndexOutOfBOundaryError_1.IndexOutOfBoundaryError();
            }
            return array[i];
        }
        async length() {
            return array.length;
        }
        async indexOf(a) {
            throw new MethodNotImplementedError_1.MethodNotImplementedError();
        }
        async pop() {
            if (!array.length) {
                throw new Error('empty array');
            }
            return array.pop();
        }
        async shift() {
            if (!array.length) {
                throw new Error('empty array');
            }
            return array.shift();
        }
    })(system);
    return _array;
}
function wrapUint8Array(array, system) {
    const _array = new (class Array extends _1.$ {
        constructor() {
            super(...arguments);
            this.__ = ['A'];
        }
        buffer() {
            return array.buffer;
        }
        read() {
            return [...array];
        }
        write(data) {
            array.set(data);
        }
        async append(a) {
            array[array.length] = a;
            return;
        }
        async put(i, data) {
            array[i] = data;
        }
        async at(i) {
            return array[i];
        }
        async length() {
            return array.length;
        }
        async indexOf(a) {
            throw new MethodNotImplementedError_1.MethodNotImplementedError();
        }
        async set(array_, offset) {
            array.set(array_, offset);
        }
        async pop() {
            if (array.length === 0) {
                throw new Error('empty array');
            }
            const last = array[array.length - 1];
            array = array.subarray(0, array.length - 1);
            return Promise.resolve(last);
        }
        shift() {
            if (array.length === 0) {
                throw new Error('empty array');
            }
            const first = array[0];
            array = array.subarray(1, array.length);
            return Promise.resolve(first);
        }
        raw() {
            return array;
        }
    })(system);
    return _array;
}
//# sourceMappingURL=Array.js.map