export const Kind = 'Core#License';
export class License {
    kind = Kind;
    /**
     * The URL to the license text.
     * Only `url` or `content` can be used at a time.
     */
    url;
    /**
     * The name of the license.
     */
    name;
    /**
     * The content of the license.
     * Only `url` or `content` can be used at a time.
     */
    content;
    static fromUrl(url, name) {
        return new License({
            kind: Kind,
            url,
            name,
        });
    }
    static fromContent(content, name) {
        return new License({
            kind: Kind,
            content,
            name,
        });
    }
    /**
     * @param input The license definition used to restore the state.
     */
    constructor(input) {
        let init;
        if (typeof input === 'string') {
            init = JSON.parse(input);
        }
        else if (typeof input === 'object') {
            init = input;
        }
        else {
            init = {
                kind: Kind,
            };
        }
        this.new(init);
    }
    /**
     * Creates a new license clearing anything that is so far defined.
     *
     * Note, this throws an error when the license is not a license object.
     */
    new(init) {
        if (!License.isLicense(init)) {
            throw new Error(`Not a license.`);
        }
        const { url, content, name } = init;
        this.kind = Kind;
        this.name = name;
        this.content = content;
        this.url = url;
    }
    /**
     * Checks whether the input is a definition of a license.
     */
    static isLicense(input) {
        const typed = input;
        if (typed && typed.kind === Kind) {
            return true;
        }
        return false;
    }
    toJSON() {
        const result = {
            kind: Kind,
        };
        if (this.name) {
            result.name = this.name;
        }
        if (this.url) {
            result.url = this.url;
        }
        if (this.content) {
            result.content = this.content;
        }
        return result;
    }
}
//# sourceMappingURL=License.js.map