/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module QuantityFormatting
 */
import { BaseUnitFormattingSettingsProvider } from "./BaseUnitFormattingSettingsProvider";
/** Implementation of BaseUnitFormattingSettingsProvider that stores and retrieves data in local storage.
 *  @beta
 */
export class LocalUnitFormatProvider extends BaseUnitFormattingSettingsProvider {
    /** If `maintainOverridesPerIModel` is true, the base class will set up listeners to monitor active iModel
     *  changes so the overrides for the QuantityFormatter properly match the overrides set up by the user. */
    constructor(quantityFormatter, maintainOverridesPerIModel) {
        super(quantityFormatter, maintainOverridesPerIModel);
    }
    buildUnitSystemKey() {
        if (this.imodelConnection)
            return `unitsystem#i:${this.imodelConnection.iModelId}`;
        return `unitsystem#user`;
    }
    async retrieveUnitSystem(defaultKey) {
        const readUnitSystem = localStorage.getItem(this.buildUnitSystemKey());
        if (readUnitSystem && readUnitSystem.length) {
            return readUnitSystem;
        }
        return defaultKey;
    }
    async storeUnitSystemKey(unitSystemKey) {
        try {
            localStorage.setItem(this.buildUnitSystemKey(), unitSystemKey);
            return true;
        }
        catch (_e) {
            return false;
        }
    }
    buildOverridesKey(quantityTypeKey) {
        if (this.imodelConnection)
            return `quantityTypeFormat#i:${this.imodelConnection.iModelId}#q:${quantityTypeKey}`;
        return `quantityTypeFormat#user#q:${quantityTypeKey}`;
    }
    async store(quantityTypeKey, overrideProps) {
        try {
            localStorage.setItem(this.buildOverridesKey(quantityTypeKey), JSON.stringify(overrideProps));
            return true;
        }
        catch (_e) {
            return false;
        }
    }
    async retrieve(quantityTypeKey) {
        const storedFormat = localStorage.getItem(this.buildOverridesKey(quantityTypeKey));
        if (storedFormat) {
            return JSON.parse(storedFormat);
        }
        return undefined;
    }
    async remove(quantityTypeKey) {
        const key = this.buildOverridesKey(quantityTypeKey);
        if (localStorage.getItem(key)) {
            localStorage.removeItem(key);
            return true;
        }
        return false;
    }
}
//# sourceMappingURL=LocalUnitFormatProvider.js.map