"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Image = void 0;
const col_cache_1 = require("./utils/col-cache.js");
const anchor_1 = require("./anchor.js");
class Image {
    constructor(worksheet, model) {
        this.worksheet = worksheet;
        if (model) {
            this.model = model;
        }
    }
    get model() {
        switch (this.type) {
            case "background":
                return {
                    type: this.type,
                    imageId: this.imageId
                };
            case "image":
                return {
                    type: this.type,
                    imageId: this.imageId,
                    hyperlinks: this.range.hyperlinks,
                    range: {
                        tl: this.range.tl.model,
                        br: this.range.br && this.range.br.model,
                        ext: this.range.ext,
                        editAs: this.range.editAs
                    }
                };
            default:
                throw new Error("Invalid Image Type");
        }
    }
    set model({ type, imageId, range, hyperlinks }) {
        this.type = type;
        this.imageId = imageId;
        if (type === "image") {
            if (typeof range === "string") {
                const decoded = col_cache_1.colCache.decode(range);
                if ("top" in decoded) {
                    // It's a Location (range like "A1:C3")
                    this.range = {
                        tl: new anchor_1.Anchor(this.worksheet, { col: decoded.left, row: decoded.top }, -1),
                        br: new anchor_1.Anchor(this.worksheet, { col: decoded.right, row: decoded.bottom }, 0),
                        editAs: "oneCell"
                    };
                }
            }
            else if (range) {
                this.range = {
                    tl: new anchor_1.Anchor(this.worksheet, range.tl, 0),
                    br: range.br ? new anchor_1.Anchor(this.worksheet, range.br, 0) : undefined,
                    ext: range.ext,
                    editAs: range.editAs,
                    hyperlinks: hyperlinks || ("hyperlinks" in range ? range.hyperlinks : undefined)
                };
            }
        }
    }
    clone(worksheet) {
        const target = worksheet ?? this.worksheet;
        const cloned = new Image(target);
        cloned.type = this.type;
        cloned.imageId = this.imageId;
        if (this.range) {
            cloned.range = {
                tl: this.range.tl.clone(target),
                br: this.range.br ? this.range.br.clone(target) : undefined,
                ext: this.range.ext ? { ...this.range.ext } : undefined,
                editAs: this.range.editAs,
                hyperlinks: this.range.hyperlinks ? { ...this.range.hyperlinks } : undefined
            };
        }
        return cloned;
    }
}
exports.Image = Image;
