"use strict";
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module Elements
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.Texture = void 0;
const core_common_1 = require("@itwin/core-common");
const Element_1 = require("./Element");
/** Defines a rendering texture that is associated with a Material and applied to surface geometry.
 * @public @preview
 */
class Texture extends Element_1.DefinitionElement {
    /** @internal */
    static get className() { return "Texture"; }
    format;
    data;
    description;
    /** @beta */
    constructor(props, iModel) {
        super(props, iModel);
        this.format = props.format;
        this.data = typeof props.data === "string" ? core_common_1.Base64EncodedString.toUint8Array(props.data) : props.data;
        this.description = props.description;
    }
    toJSON() {
        const val = super.toJSON();
        val.format = this.format;
        val.data = core_common_1.Base64EncodedString.fromUint8Array(this.data);
        val.description = this.description;
        return val;
    }
    /** Create a Code for a Texture given a name that is meant to be unique within the scope of the specified DefinitionModel.
     * @param iModel  The IModelDb
     * @param scopeModelId The Id of the DefinitionModel that contains the Texture and provides the scope for its name.
     * @param name The Texture name
     */
    static createCode(iModel, scopeModelId, name) {
        const codeSpec = iModel.codeSpecs.getByName(core_common_1.BisCodeSpec.texture);
        return 0 === name.length ? core_common_1.Code.createEmpty() : new core_common_1.Code({ spec: codeSpec.id, scope: scopeModelId, value: name });
    }
    /** Create a texture with the given parameters.
     * @param iModelDb The iModel to contain the texture.
     * @param definitionModelId The [[DefinitionModel]] to contain the texture.
     * @param name The name to serve as the texture's [Code]($common) value.
     * @param format The format of the image data.
     * @param data The image data in the format specified by `format`.
     * @param description An optional description of the texture
     * @returns The newly constructed Texture element.
     * @throws [[IModelError]] if unable to create the element.
     * @see [[insertTexture]] to insert a new texture into the iModel.
     */
    static createTexture(iModelDb, definitionModelId, name, format, data, description) {
        const textureProps = {
            classFullName: this.classFullName,
            code: this.createCode(iModelDb, definitionModelId, name),
            format,
            data,
            description,
            model: definitionModelId,
            isPrivate: false,
        };
        return new Texture(textureProps, iModelDb);
    }
    /** Insert a new texture into a [[DefinitionModel]].
     * @param iModelDb The iModel to contain the texture.
     * @param definitionModelId The [[DefinitionModel]] to contain the texture.
     * @param name The name to serve as the texture's [Code]($common) value.
     * @param format The format of the image data.
     * @param data The image data in the format specified by `format`.
     * @param description An optional description of the texture
     * @returns The Id of the newly-inserted texture element.
     * @throws [[IModelError]] if unable to insert the element.
     * @see [[insertTexture]] to insert a new texture into the iModel.
     */
    static insertTexture(iModelDb, definitionModelId, name, format, data, description) {
        const texture = this.createTexture(iModelDb, definitionModelId, name, format, data, description);
        return iModelDb.elements.insertElement(texture.toJSON());
    }
}
exports.Texture = Texture;
//# sourceMappingURL=Texture.js.map