var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { MapToComponentField } from 'vega-slimmer/core';
import { SubStateObserverSlimmer } from '../../../../../helpers/slimmers/sub-state/sub-state-observer-slimmer';
import { findParent } from '../../../../../utils/ui';
/**
 * Controller for the vega dropdown item states
 */
export class VegaDropdownItemStateController extends SubStateObserverSlimmer {
  // eslint-disable-next-line jsdoc/require-jsdoc
  constructor() {
    super({
      // eslint-disable-next-line jsdoc/require-jsdoc
      parent: () => {
        // we trigger this method in `componentWillLoad`.
        // if dropdown is template mode, the `vega-dropdown-content-box` have not been rendered, but we can find dropdown directly.
        // if dropdown is source mode, the `vega-dropdown-content-box` load before dropdown-item load, we can through `vega-dropdown-content-box` to find vega-dropdown
        const parent = findParent(this.host, 'vega-dropdown');
        if (parent) {
          return parent;
        }
        else {
          const appender = findParent(this.host, 'vega-dropdown-content-box');
          /* istanbul ignore next */
          // TODO: to add test for throw, since there is not better function to handle `componentWillLoad` in spec/e2e test.
          if (appender && appender['data-parent-ref']) {
            return appender['data-parent-ref'];
          }
        }
      },
      subStates: [
        {
          subStateName: 'selectType',
          parentStateName: 'selectType',
        },
        {
          subStateName: 'prefixIconSlot',
          parentStateName: 'prefixIconSlot',
        },
        {
          subStateName: 'itemDisplayRule',
          parentStateName: 'itemDisplayRule',
        },
        {
          subStateName: 'itemTooltipProps',
          parentStateName: 'itemTooltipProps',
        },
        {
          subStateName: 'selected',
          parentStateName: 'selectedSourceKey',
          // eslint-disable-next-line jsdoc/require-jsdoc
          callback: (selectedSourceKey) => {
            this.selected = Array.isArray(selectedSourceKey)
              ? selectedSourceKey.includes(this.itemKey)
              : selectedSourceKey === this.itemKey;
          },
        },
      ],
      pullStatesWhenReattached: true,
    });
  }
}
__decorate([
  MapToComponentField()
], VegaDropdownItemStateController.prototype, "host", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaDropdownItemStateController.prototype, "selectType", void 0);
__decorate([
  MapToComponentField()
], VegaDropdownItemStateController.prototype, "itemKey", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaDropdownItemStateController.prototype, "selected", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaDropdownItemStateController.prototype, "prefixIconSlot", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaDropdownItemStateController.prototype, "itemDisplayRule", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaDropdownItemStateController.prototype, "itemTooltipProps", void 0);
