/**
 * Do not modify this file, it is auto-generated.
 */
import { Observable, OperatorFunction } from 'rxjs';
import { map } from 'rxjs/operators';
import { Generator } from '@ibm-wch-sdk/utils';

import {
    {{#each imports}}
    {{{this}}},
    {{/each}}
    RenderingContext
} from '@ibm-wch-sdk/ng';
{{#each otherImports}}
import { {{#each this}}{{{this}}}{{#unless @last}}, {{/unless}}{{/each}} } from '{{{@key}}}';
{{/each}}

/*
{{#if name}}
 * @name {{{name}}}
{{/if}}
{{#if id}}
 * @id {{{id}}}
{{/if}}
{{#if description}}
 * @description {{{description}}}
{{/if}}
{{#if creator}}
 * @author {{{creator}}}
{{/if}}
 */
export interface {{{interfacesName}}} extends RenderingContext {

    /*
     * The ID of the content type this item belongs to.
     */
    typeId: '{{{jsEscape id }}}';

    /*
     * this is the link to the content type document this content is based on.
     */
    type: '{{{jsEscape name }}}';

    /*
     * The classification defines the document type. For content items, all documents are classified as \"content\".
     */
    classification: 'content';

    /*
     * Defined by the type and capture in the schema given by the type,
     *  in a real content, this property will be filled with more information.
     */
    elements: {{{definitionName}}};
{{#each expressions}}

    {{{@key}}}: {
{{#each this}}
    /**
{{{this.comment}}}
    */
    ['{{{jsEscape @key}}}']{{#if this.optional}}?{{/if}}: {{{this.type}}};
{{/each}}
    };
{{/each}}

}

/** Validates that the context is indeed of the desired type
 *
 * @param aContext instance of the {@link RenderingContext} to check
 * @return true if the context is a {@link {{ interfacesName }} } else false
 */
export function is{{{ interfacesName }}}(aContext: RenderingContext): aContext is {{{ interfacesName }}} {
    return !!aContext && is{{{definitionName}}}(aContext.elements);
}

/** Provides a type assertion that can be used to validate and convert a generic {@link RenderingContext}
 * info a {@link {{ interfacesName }} }
 *
 * @param aContext instance of the rendering context to check
 * @return the {@link {{ interfacesName }} } version of the {@link RenderingContext} or an exception
 *
 * @example this.onRenderingContext.map(assert{{{ interfacesName }}});
 */
export function assert{{{ interfacesName }}}(aContext: RenderingContext): {{{ interfacesName }}} {
    // test if the context is as expected
    if (is{{{ interfacesName }}}(aContext)) {
        return aContext;
    }
    // type failure
    throw new TypeError('{{{jsEscape interfacesName}}}');
}

/** Operator to convert a {@link RenderingContext} into a {@link {{ interfacesName }} } using a pipe.
 *
 * @example this.onRenderingContext.pipe(op{{{ interfacesName }}}());
 */
export const op{{{ interfacesName }}}: Generator<OperatorFunction<RenderingContext, {{{ interfacesName }}}>>  = () => map<RenderingContext, {{{ interfacesName }}}>(assert{{{ interfacesName }}});