"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
var NinePatch = /** @class */ (function (_super) {
    __extends(NinePatch, _super);
    function NinePatch(game, x, y, key, frame, width, height, data) {
        var _this = _super.call(this) || this;
        _this.game = game;
        _this.x = x;
        _this.y = y;
        _this.anchor = new Phaser.Point();
        _this.internalLoadTexture(key, frame);
        if (frame !== null && !data) {
            data = game.cache.getNinePatch(frame);
        }
        else if (!data) {
            data = game.cache.getNinePatch(key);
        }
        if (undefined === data) {
            return _this;
        }
        _this.topSize = data.top;
        _this.leftSize = data.left || _this.topSize;
        _this.rightSize = data.right || _this.leftSize;
        _this.bottomSize = data.bottom || _this.topSize;
        _this.patches = [];
        _this.resize(width, height);
        return _this;
    }
    // eslint-disable-next-line class-methods-use-this
    NinePatch.prototype.update = function () { };
    // eslint-disable-next-line class-methods-use-this
    NinePatch.prototype.postUpdate = function () { };
    NinePatch.prototype.loadTexture = function (key, frame) {
        this.internalLoadTexture(key, frame);
        this.flushPatches();
        this.drawPatches();
    };
    Object.defineProperty(NinePatch.prototype, "frameName", {
        set: function (frame) {
            this.internalSetFrame(this.baseTexture.source.name, frame);
            this.flushPatches();
            this.drawPatches();
        },
        enumerable: true,
        configurable: true
    });
    NinePatch.prototype.resize = function (width, height) {
        this.localWidth = Math.round(width);
        this.localHeight = Math.round(height);
        this.drawPatches();
    };
    NinePatch.prototype.setAnchor = function (x, y) {
        this.anchor.set(x, y);
        this.drawPatches();
    };
    NinePatch.prototype.destroy = function () {
        this.parent.removeChild(this);
        this.baseTexture = null;
        this.baseFrame = null;
    };
    NinePatch.prototype.flushPatches = function () {
        this.patches.forEach(function (p) {
            p.destroy();
            p.texture.destroy(false);
        });
        this.patches.length = 0;
    };
    NinePatch.prototype.internalLoadTexture = function (key, frame) {
        this.baseTexture = this.game.cache.getBaseTexture(key);
        this.internalSetFrame(key, frame);
    };
    NinePatch.prototype.internalSetFrame = function (key, frame) {
        this.baseFrame = frame
            ? this.game.cache.getFrameByName(key, frame).getRect()
            : new Phaser.Rectangle(0, 0, this.baseTexture.width, this.baseTexture.height);
    };
    NinePatch.prototype.drawPatches = function () {
        // The positions we want from the base texture
        var textureXs = [0, this.leftSize, this.baseFrame.width - this.rightSize, this.baseFrame.width];
        var textureYs = [0, this.topSize, this.baseFrame.height - this.bottomSize, this.baseFrame.height];
        // These are the positions we need the eventual texture to have
        var finalXs = [0, this.leftSize, this.localWidth - this.rightSize, this.localWidth];
        var finalYs = [0, this.topSize, this.localHeight - this.bottomSize, this.localHeight];
        for (var yi = 0; yi < 3; yi++) {
            for (var xi = 0; xi < 3; xi++) {
                var s = this.createPatch(xi + yi * 3, textureXs[xi], // x
                textureYs[yi], // y
                textureXs[xi + 1] - textureXs[xi], // width
                textureYs[yi + 1] - textureYs[yi] // height,
                );
                s.width = finalXs[xi + 1] - finalXs[xi];
                s.height = finalYs[yi + 1] - finalYs[yi];
                s.x = finalXs[xi] - this.localWidth * this.anchor.x;
                s.y = finalYs[yi] - this.localHeight * this.anchor.y;
                this.addChild(s);
            }
        }
    };
    NinePatch.prototype.createPatch = function (index, x, y, width, height) {
        var patch = this.patches[index];
        if (patch) {
            return patch;
        }
        var frame = new PIXI.Rectangle(Math.floor(this.baseFrame.x + x), Math.floor(this.baseFrame.y + y), Math.floor(Math.max(width, 1)), Math.floor(Math.max(height, 1)));
        var t = new PIXI.Texture(this.baseTexture, frame);
        patch = new Phaser.Image(this.game, 0, 0, t);
        this.patches.push(patch);
        return patch;
    };
    return NinePatch;
}(PIXI.DisplayObjectContainer));
exports.NinePatch = NinePatch;
//# sourceMappingURL=NinePatch.js.map