/*!
 * Copyright (c) 2026 Vertex Software LLC. All rights reserved.
 */
import { Color, EventDispatcher } from "@vertexvis/utils";
export function isTextPin(pin) {
    return (pin === null || pin === void 0 ? void 0 : pin.type) === 'text';
}
export function isIconPin(pin) {
    return (pin === null || pin === void 0 ? void 0 : pin.type) === 'icon';
}
function convertColorToString(color) {
    return typeof color === 'string' ? color : Color.toHexString(color);
}
export function getPinColors(pin) {
    var _a, _b, _c, _d, _e, _f, _g, _h;
    return {
        primaryColor: ((_b = (_a = pin === null || pin === void 0 ? void 0 : pin.attributes) === null || _a === void 0 ? void 0 : _a.style) === null || _b === void 0 ? void 0 : _b.primaryColor) == null
            ? undefined
            : convertColorToString((_d = (_c = pin === null || pin === void 0 ? void 0 : pin.attributes) === null || _c === void 0 ? void 0 : _c.style) === null || _d === void 0 ? void 0 : _d.primaryColor),
        accentColor: ((_f = (_e = pin === null || pin === void 0 ? void 0 : pin.attributes) === null || _e === void 0 ? void 0 : _e.style) === null || _f === void 0 ? void 0 : _f.accentColor) == null
            ? undefined
            : convertColorToString((_h = (_g = pin === null || pin === void 0 ? void 0 : pin.attributes) === null || _g === void 0 ? void 0 : _g.style) === null || _h === void 0 ? void 0 : _h.accentColor),
    };
}
export class PinModel {
    constructor() {
        this.entities = {};
        this.entityAdded = new EventDispatcher();
        this.entityUpdated = new EventDispatcher();
        this.entitiesChanged = new EventDispatcher();
        this.selectionChanged = new EventDispatcher();
    }
    /**
     * Registers an entity to be drawn in the canvas
     *
     * @param pin A pin entity to draw.
     * @returns `true` if the entity has been added.
     */
    addPin(pin, suppressEvent = false) {
        if (this.entities[pin.id] == null) {
            this.entities = {
                ...this.entities,
                [pin.id]: pin,
            };
            if (!suppressEvent) {
                this.entitiesChanged.emit(this.getPins());
                this.entityAdded.emit(pin);
            }
            return true;
        }
        else {
            return false;
        }
    }
    /**
     * Clears all registered entities from the model.
     */
    clearPins() {
        this.getPins().forEach((e) => this.removePin(e));
    }
    /**
     * Returns all the entities registered with the model.
     */
    getPins() {
        return Object.keys(this.entities).map((key) => this.entities[key]);
    }
    /**
     * Returns single entity by id if present in the model.
     */
    getPinById(id) {
        return this.entities[id];
    }
    getSelectedPinId() {
        return this.selectedPinId;
    }
    /**
     * Unregisters an entity from the model.
     *
     * @param entity The entity to remove.
     * @returns `true` if the entity was removed.
     */
    removePin(entity) {
        if (this.entities[entity.id] != null) {
            delete this.entities[entity.id];
            this.entitiesChanged.emit(this.getPins());
            return true;
        }
        else {
            return false;
        }
    }
    /**
     * Sets the set of entities to be placed with the model.
     *
     * @param entities A set of entities to draw.
     * @returns `true` if the entity has been added.
     */
    setPins(pins) {
        this.clearPins();
        pins.forEach((e) => this.addPin(e, true));
        this.entitiesChanged.emit(this.getPins());
        return true;
    }
    /**
     * Sets the set of entities to be placed with the model.
     *
     * @param pin A pin to set
     * @returns `true` if the entity has been set
     */
    setPin(pin) {
        const isNewPin = this.entities[pin.id] == null;
        this.entities = {
            ...this.entities,
            [pin.id]: pin,
        };
        this.entitiesChanged.emit(this.getPins());
        if (isNewPin) {
            this.entityAdded.emit(pin);
        }
        return true;
    }
    /**
     * Updates a pin in the model.
     *
     * @param pin A pin to update
     * @returns `true` if the entity has been updated
     */
    updatePin(pin) {
        const pinById = this.getPinById(pin.id);
        if (pinById != null) {
            this.entities = {
                ...this.entities,
                [pin.id]: pin,
            };
            this.entitiesChanged.emit(this.getPins());
            this.entityUpdated.emit(pin);
            return true;
        }
        return false;
    }
    /**
     * Registers an event listener that will be invoked when the model's
     * pin entities change.
     *
     * @param listener The listener to add.
     * @returns A disposable that can be used to remove the listener.
     */
    onEntitiesChanged(listener) {
        return this.entitiesChanged.on(listener);
    }
    /**
     * Registers an event listener that will be invoked when a new pin
     * is added to the model.
     *
     * @param listener The listener to add.
     * @returns A disposable that can be used to remove the listener.
     */
    onEntityAdded(listener) {
        return this.entityAdded.on(listener);
    }
    /**
     * Registers an event listener that will be invoked when a pin
     * is updated in the model.
     *
     * @param listener The listener to add.
     * @returns A disposable that can be used to remove the listener.
     */
    onEntityUpdated(listener) {
        return this.entityUpdated.on(listener);
    }
    /**
     * Registers an event listener that will be invoked when the model's
     * pin selection changes
     *
     * @param listener The listener to add.
     * @returns A disposable that can be used to remove the listener.
     */
    onSelectionChange(listener) {
        return this.selectionChanged.on(listener);
    }
    /**
     * Sets the selected pin id
     * @param pinId
     */
    setSelectedPin(pinId) {
        this.selectedPinId = pinId;
        this.selectionChanged.emit(this.selectedPinId);
    }
}
//# sourceMappingURL=model.js.map
