"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var IPatchesConfig_1 = require("./IPatchesConfig");
var NinePatch = (function (_super) {
    tslib_1.__extends(NinePatch, _super);
    function NinePatch(scene, x, y, width, height, key, frame, config) {
        var _this = _super.call(this, scene, x, y, Math.max(width, 2), Math.max(height, 2)) || this;
        _this.setOrigin(0.5, 0.5);
        _this.config = config || _this.scene.cache.custom.ninePatch.get(frame ? "" + frame : key);
        IPatchesConfig_1.normalizePatchesConfig(_this.config);
        _this.setTexture(key, frame);
        _this.drawPatches();
        return _this;
    }
    NinePatch.prototype.resize = function (width, height) {
        if (!this.config) {
            _super.prototype.resize.call(this, width, height);
            return this;
        }
        if (this.width === width && this.height === height) {
            return this;
        }
        width = Math.max(width, this.config.left + this.config.right);
        height = Math.max(height, this.config.top + this.config.bottom);
        _super.prototype.resize.call(this, width, height);
        this.drawPatches();
        return this;
    };
    NinePatch.prototype.setTexture = function (key, frame) {
        this.originTexture = this.scene.textures.get(key);
        this.setFrame(frame);
        return this;
    };
    NinePatch.prototype.setFrame = function (frame) {
        this.originFrame = this.originTexture.frames[frame] || this.originTexture.frames[NinePatch.__BASE];
        this.drawPatches();
        return this;
    };
    NinePatch.prototype.drawPatches = function () {
        var textureXs = [0, this.config.left, this.originFrame.width - this.config.right, this.originFrame.width];
        var textureYs = [0, this.config.top, this.originFrame.height - this.config.bottom, this.originFrame.height];
        var finalXs = [0, this.config.left, this.width - this.config.right, this.width];
        var finalYs = [0, this.config.top, this.height - this.config.bottom, this.height];
        var patchIndex = 0;
        for (var yi = 0; yi < 3; yi++) {
            for (var xi = 0; xi < 3; xi++) {
                var patch = this.createPatchFrame(NinePatch.patches[patchIndex], textureXs[xi], textureYs[yi], textureXs[xi + 1] - textureXs[xi], textureYs[yi + 1] - textureYs[yi]);
                var patchImg = this.scene.make.image({ x: 0, y: 0, key: patch.texture.key, frame: patch.name });
                patchImg.setOrigin(0);
                patchImg.setScale((finalXs[xi + 1] - finalXs[xi]) / patch.width, (finalYs[yi + 1] - finalYs[yi]) / patch.height);
                this.draw(patchImg, finalXs[xi], finalYs[yi]);
                patchImg.destroy();
                ++patchIndex;
            }
        }
    };
    NinePatch.prototype.createPatchFrame = function (patch, x, y, width, height) {
        var name = this.originFrame.name + "|" + patch;
        if (!this.originTexture.frames.hasOwnProperty(name)) {
            return this.originTexture.add(name, this.originFrame.sourceIndex, this.originFrame.cutX + x, this.originFrame.cutY + y, width, height);
        }
        return this.originTexture.frames[name];
    };
    NinePatch.__BASE = "__BASE";
    NinePatch.patches = ["[0][0]", "[1][0]", "[2][0]", "[0][1]", "[1][1]", "[2][1]", "[0][2]", "[1][2]", "[2][2]"];
    return NinePatch;
}(Phaser.GameObjects.RenderTexture));
exports.NinePatch = NinePatch;
//# sourceMappingURL=NinePatch.js.map