import { assert } from "../assert.js";
import { ResourceAccessKind } from "./ResourceAccessKind.js";

const DEFAULT_ACCESS = ResourceAccessKind.Read | ResourceAccessKind.Write;

/**
 * @template R
 */
export class ResourceAccessSpecification {
    /**
     *
     * @type {number|ResourceAccessKind}
     */
    access = DEFAULT_ACCESS

    /**
     *
     * @type {R|null}
     */
    resource = null

    /**
     * @template R
     * @param {R} resource
     * @param {number|ResourceAccessKind} access
     * @returns {ResourceAccessSpecification<R>}
     */
    static from(resource, access = DEFAULT_ACCESS) {
        assert.defined(resource, 'resource');
        assert.notNull(resource, 'resource');

        assert.isNonNegativeInteger(access, 'access');

        const r = new ResourceAccessSpecification();

        r.access = access;
        r.resource = resource;

        return r;
    }
}

/**
 * @readonly
 * @type {boolean}
 */
ResourceAccessSpecification.prototype.isResourceAccessSpecification = true;
