"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 DisplayStyles
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.HiddenLine = void 0;
const core_bentley_1 = require("@itwin/core-bentley");
const ColorDef_1 = require("./ColorDef");
const LinePixels_1 = require("./LinePixels");
/** Namespace containing types controlling how edges and surfaces should be drawn in "hidden line" and "solid fill" [[RenderMode]]s.
 * @public
 */
var HiddenLine;
(function (HiddenLine) {
    /** Describes the symbology with which edges should be drawn. */
    class Style {
        /** @internal */
        get ovrColor() { return undefined !== this.color; }
        constructor(json, hidden) {
            if (core_bentley_1.JsonUtils.isEmptyObjectOrUndefined(json)) {
                if (hidden)
                    this.pattern = LinePixels_1.LinePixels.HiddenLine;
                return;
            }
            json = json; // per JsonUtils.isEmptyObjectOrUndefined()
            if (undefined !== json.color && false !== json.ovrColor)
                this.color = ColorDef_1.ColorDef.fromJSON(json.color);
            if (undefined !== json.pattern) {
                const pattern = core_bentley_1.JsonUtils.asInt(json.pattern, hidden ? LinePixels_1.LinePixels.HiddenLine : LinePixels_1.LinePixels.Invalid);
                if (LinePixels_1.LinePixels.Invalid !== pattern)
                    this.pattern = pattern;
            }
            else if (hidden) {
                this.pattern = LinePixels_1.LinePixels.HiddenLine;
            }
            if (undefined !== json.width) {
                let width = core_bentley_1.JsonUtils.asInt(json.width, 0);
                if (0 !== width) {
                    width = Math.max(1, width);
                    this.width = Math.min(32, width);
                }
            }
        }
        static fromJSON(json, hidden) {
            if (undefined !== json)
                return new Style(json, hidden);
            return hidden ? this.defaultHidden : this.defaultVisible;
        }
        /** Create a Style equivalent to this one but with the specified color override. */
        overrideColor(color) {
            if (undefined === this.color && undefined === color)
                return this;
            if (undefined !== this.color && undefined !== color && this.color.equals(color))
                return this;
            return Style.fromJSON({
                color: color?.toJSON(),
                ovrColor: undefined !== color,
                pattern: this.pattern,
                width: this.width,
            });
        }
        /** Create a Style equivalent to this one but with the specified pattern override. */
        overridePattern(pattern) {
            if (pattern === this.pattern)
                return this;
            return Style.fromJSON({
                color: this.color?.toJSON(),
                ovrColor: this.ovrColor,
                pattern,
                width: this.width,
            });
        }
        /** Create a Style equivalent to this one but with the specified width override. */
        overrideWidth(width) {
            if (width === this.width)
                return this;
            return Style.fromJSON({
                color: this.color?.toJSON(),
                ovrColor: this.ovrColor,
                pattern: this.pattern,
                width,
            });
        }
        /** Returns true if this Style is equivalent to the supplied Style. */
        equals(other) {
            if (this === other)
                return true;
            else if (this.ovrColor !== other.ovrColor || this.pattern !== other.pattern || this.width !== other.width)
                return false;
            else
                return undefined === this.color || this.color.equals(other.color);
        }
        toJSON() {
            return {
                ovrColor: this.ovrColor,
                color: this.color ? this.color.toJSON() : ColorDef_1.ColorDef.white.toJSON(),
                pattern: undefined !== this.pattern ? this.pattern : LinePixels_1.LinePixels.Invalid,
                width: undefined !== this.width ? this.width : 0,
            };
        }
    }
    Style.defaultVisible = new Style({});
    Style.defaultHidden = new Style({}, true);
    HiddenLine.Style = Style;
    /** Describes how visible and hidden edges and transparent surfaces should be rendered in "hidden line" and "solid fill" [[RenderMode]]s. */
    class Settings {
        /** An alias for [[transparencyThreshold]]. */
        get transThreshold() { return this.transparencyThreshold; }
        /** Create a DisplaySettings from its JSON representation. */
        static fromJSON(json) {
            if (core_bentley_1.JsonUtils.isEmptyObjectOrUndefined(json))
                return this.defaults;
            else if (json instanceof Settings)
                return json;
            else
                return new Settings(json);
        }
        toJSON() {
            const props = {
                visible: this.visible.toJSON(),
                hidden: this.hidden.toJSON(),
                transThreshold: this.transThreshold,
            };
            return props;
        }
        /** Create a Settings equivalent to this one with the exception of those properties defined in the supplied JSON. */
        override(props) {
            const visible = props.visible;
            const hidden = props.hidden;
            const transparencyThreshold = props.transThreshold;
            return Settings.fromJSON({
                visible: undefined !== visible ? visible : this.visible.toJSON(),
                hidden: undefined !== hidden ? hidden : this.hidden.toJSON(),
                transThreshold: undefined !== transparencyThreshold ? transparencyThreshold : this.transparencyThreshold,
            });
        }
        equals(other) {
            if (this === other)
                return true;
            return this.visible.equals(other.visible)
                && this.hidden.equals(other.hidden)
                && this.transparencyThreshold === other.transparencyThreshold;
        }
        get matchesDefaults() {
            return this.equals(Settings.defaults);
        }
        constructor(json) {
            this.visible = Style.fromJSON(json.visible);
            this.hidden = Style.fromJSON(json.hidden, true);
            this.transparencyThreshold = core_bentley_1.JsonUtils.asDouble(json.transThreshold, 1.0);
        }
    }
    /** The default display settings. */
    Settings.defaults = new Settings({});
    HiddenLine.Settings = Settings;
})(HiddenLine || (exports.HiddenLine = HiddenLine = {}));
//# sourceMappingURL=HiddenLine.js.map