"use strict";

import React, { Component } from 'react';
import { Button, TouchableOpacity } from 'react-native';
import { Navigation } from 'react-native-navigation';
import { events } from "../Stores/EventsStore.js";
import { jsx as _jsx } from "react/jsx-runtime";
export const NavigationButton = class extends Component {
  ref = undefined;
  render() {
    const {
      button,
      componentId
    } = this.props;
    if (button.component) return this.renderButtonComponent();
    return /*#__PURE__*/_jsx(Button, {
      testID: button.testID,
      title: button.text || '',
      disabled: button.enabled === false,
      onPress: () => button.enabled !== false && events.invokeNavigationButtonPressed({
        buttonId: button.id,
        componentId
      })
    }, button.id);
  }
  renderButtonComponent() {
    const {
      button,
      componentId
    } = this.props;
    // @ts-ignore
    const buttonComponentId = button.component.componentId;
    // @ts-ignore
    const ComponentClass = Navigation.mock.store.getComponentClassForName(button.component.name);
    if (!ComponentClass) {
      throw new Error(`Cannot find registered component for: ${button.component?.name}`);
    }
    const ButtonComponent = ComponentClass();
    const props = Navigation.mock.store.getPropsForId(buttonComponentId);
    return /*#__PURE__*/_jsx(TouchableOpacity, {
      onPress: () => {
        if (this.ref) {
          this.invokeOnClick(
          // @ts-ignore
          (this.ref._reactInternalFiber || this.ref._reactInternals).return.stateNode);
        }
        events.invokeNavigationButtonPressed({
          buttonId: button.id,
          componentId: componentId
        });
      },
      testID: button.testID,
      children: /*#__PURE__*/_jsx(ButtonComponent, {
        ...props,
        componentId: buttonComponentId,
        ref: ref => this.ref = ref
      }, buttonComponentId)
    });
  }
  invokeOnClick(stateNode) {
    if (stateNode.children) {
      // @ts-ignore
      stateNode.children.forEach(instance => {
        if (instance.internalInstanceHandle && instance.internalInstanceHandle.stateNode.props.onClick) {
          instance.internalInstanceHandle.stateNode.props.onClick();
        }
        this.invokeOnClick(instance);
      });
    }
  }
};
//# sourceMappingURL=NavigationButton.js.map