/*---------------------------------------------------------------------------------------------
* 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
 */
import { Base64EncodedString, BisCodeSpec, Code, } from "@itwin/core-common";
import { DefinitionElement } from "./Element";
import { EditTxn } from "./EditTxn";
import { _implicitTxn } from "./internal/Symbols";
/** Defines a rendering texture that is associated with a Material and applied to surface geometry.
 * @public @preview
 */
export class Texture extends 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" ? Base64EncodedString.toUint8Array(props.data) : props.data;
        this.description = props.description;
    }
    toJSON() {
        const val = super.toJSON();
        val.format = this.format;
        val.data = 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(BisCodeSpec.texture);
        return 0 === name.length ? Code.createEmpty() : new 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);
    }
    static insertTexture(txnOrDb, definitionModelId, name, format, data, description) {
        const txn = txnOrDb instanceof EditTxn ? txnOrDb : txnOrDb[_implicitTxn];
        const texture = this.createTexture(txn.iModel, definitionModelId, name, format, data, description);
        return texture.insert(txn);
    }
}
//# sourceMappingURL=Texture.js.map