import { UIChartsViewBase } from './ui-charts.common';
import { optionsHandler } from './options-handlers/options-handler';
import { langHandler } from './options-handlers/lang/lang-handler';
export class UIChartsView extends UIChartsViewBase {
    constructor() {
        super(...arguments);
        this._chartInitialized = false;
    }
    onLoaded() {
        super.onLoaded();
    }
    createNativeView() {
        const chartView = new HIChartView({ frame: CGRectMake(0, 0, 200, 200) });
        // always retain delegate on owner class to ensure it doesn't inadvertently get garbage collected
        this._delegate = new HighchartsViewDelegateImpl();
        chartView.delegate = this._delegate;
        const currentVC = getVisibleViewController();
        chartView.viewController = currentVC;
        return chartView;
    }
    onUnloaded() {
        super.onUnloaded();
    }
    disposeNativeView() {
        this._chartInitialized = false;
        super.disposeNativeView();
    }
    /**
     * Initializes properties/listeners of the native view.
     */
    initNativeView() {
        // Attach the owner to nativeView.
        // When nativeView is tapped we get the owning JS object through this field.
        this.nativeView.owner = new WeakRef(this);
        super.initNativeView();
        // NSNotificationCenter.defaultCenter.addObserverForNameObjectQueueUsingBlock("valueChange", null, NSOperationQueue.mainQueue, this.onValueChange);
    }
    setOptions(opts) {
        this.options = opts;
        const hiOptions = optionsHandler(this.options);
        if (this.nativeView) {
            this.nativeView.options = hiOptions;
            this._chartInitialized = true;
            this.nativeView.reload();
        }
    }
    setLangOptions(opts) {
        const hiLang = langHandler(opts);
        if (this.nativeView) {
            this.nativeView.lang = hiLang;
        }
    }
    updateOptions(opts) {
        this.options = opts;
        const hiOptions = optionsHandler(this.options);
        if (this.nativeView) {
            this.nativeView.updateRedrawOneToOneAnimation(hiOptions, 1, 1, new HIAnimationOptionsObject());
        }
    }
    setExtremes(newMin, newMax, xAxisIndex = 0) {
        const nativeview = this.nativeView;
        if (nativeview) {
            const opts = nativeview.options;
            if (opts) {
                const xaxis = opts.xAxis.objectAtIndex(xAxisIndex);
                if (xaxis) {
                    xaxis.min = newMin;
                    xaxis.max = newMax;
                }
                nativeview.zoomOut();
                nativeview.updateRedrawOneToOneAnimation(nativeview.options, 1, 1, new HIAnimationOptionsObject());
            }
        }
    }
    enableAnnotationsModule() {
        if (this.nativeView) {
            this.nativeView.plugins = ['annotations'];
        }
    }
}
// native delegates mostly always extend NSObject
var HighchartsViewDelegateImpl = /** @class */ (function (_super) {
    __extends(HighchartsViewDelegateImpl, _super);
    function HighchartsViewDelegateImpl() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    HighchartsViewDelegateImpl.new = function () {
        return _super.new.call(this); // calls new() on the NSObject
    };
    HighchartsViewDelegateImpl.prototype.chartViewDidLoad = function (chart) {
        // console.log('HighchartsViewDelegateImpl Did load chart:', chart);
    };
    HighchartsViewDelegateImpl.ObjCProtocols = [HIChartViewDelegate]; // define our native protocalls
    return HighchartsViewDelegateImpl;
}(NSObject));
function getVisibleViewController(rootViewController) {
    if (!rootViewController) {
        const app = UIApplication.sharedApplication;
        const window = app.keyWindow || (app.windows.count > 0 && app.windows[0]);
        rootViewController = window.rootViewController;
    }
    if (rootViewController.presentedViewController) {
        return getVisibleViewController(rootViewController.presentedViewController);
    }
    if (rootViewController.isKindOfClass(UINavigationController.class())) {
        return getVisibleViewController(rootViewController.visibleViewController);
    }
    if (rootViewController.isKindOfClass(UITabBarController.class())) {
        return getVisibleViewController(rootViewController.selectedViewController);
    }
    return rootViewController;
}
