import "choerodon-ui/pro/lib/data-set/style";
import _DataSet from "choerodon-ui/pro/lib/data-set";

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

import React, { createContext, useCallback, useContext, useMemo } from 'react';
import { injectIntl } from 'react-intl';
import { inject } from 'mobx-react';
import { isEmpty } from 'lodash';
import AppVersionTableDataSet from "./AppVersionTableDataSet";
import MarketServiceListDataSet from "./MarketServiceListDataSet";
import MarketTableDataSet from "./MarketTableDataSet";
import AllMarketDataSet from "./AllMarketDataSet";
import SearchDataSet from "./SearchDataSet";
import AllMarketVersionDataSet from "./AllMarketVersionDataSet";
import useStore from "./useStore";
var Store = /*#__PURE__*/createContext({});
export function useMarketTableStore() {
  return useContext(Store);
}
export var StoreProvider = injectIntl(inject('AppState')(function (props) {
  var children = props.children,
      formatMessage = props.intl.formatMessage,
      selectedDs = props.selectedDs;
  var intlPrefix = 'c7ncd.appService.market';
  var mainStore = useStore();
  var setAppChildren = useCallback(function (record) {
    if (!record.get('childrenDataSet')) {
      var childrenDs = new _DataSet(AppVersionTableDataSet(setVersionChildren));
      record.set('childrenDataSet', childrenDs);
      childrenDs.loadData(record.get('appVersionVOS') || []);
    }
  }, []);
  var setVersionChildren = useCallback(function (record) {
    if (!record.get('childrenDataSet')) {
      var childrenDs = new _DataSet(MarketServiceListDataSet());
      record.set('childrenDataSet', childrenDs);
      childrenDs.loadData(record.get('marketServiceVOList') || []);
    }
  }, []);
  var handleMarketServiceCheck = useCallback(function (_ref) {
    var checked = _ref.checked,
        childRecord = _ref.childRecord,
        childDs = _ref.childDs,
        parentDs = _ref.parentDs,
        parentRecord = _ref.parentRecord,
        record = _ref.record;

    if (checked) {
      childDs.select(childRecord);
    } else {
      childDs.unSelect(childRecord);
    } // eslint-disable-next-line no-param-reassign


    parentRecord.isSelected = getChecked(parentRecord) && !getIndeterminate(parentRecord);
    parentRecord.set('indeterminate', getChecked(parentRecord) && getIndeterminate(parentRecord)); // eslint-disable-next-line no-param-reassign

    record.isSelected = getChecked(record) && !getIndeterminate(record);
    record.set('indeterminate', getAppIndeterminate(record));
  }, []);
  var getChecked = useCallback(function (eachRecord) {
    return eachRecord.get('childrenDataSet') && !isEmpty(eachRecord.get('childrenDataSet').selected);
  }, []);
  var getIndeterminate = useCallback(function (eachRecord) {
    var childrenDataSet = eachRecord.get('childrenDataSet');
    return !!childrenDataSet.selected.length && childrenDataSet.selected.length !== childrenDataSet.length;
  }, []);
  var getAppIndeterminate = useCallback(function (eachRecord) {
    var childrenDataSet = eachRecord.get('childrenDataSet');
    return childrenDataSet.some(function (record) {
      return record.get('indeterminate');
    }) || !!childrenDataSet.selected.length && childrenDataSet.selected.length !== childrenDataSet.length;
  }, []);
  var handleAppExpand = useCallback(function (expanded, record) {
    if (expanded && !record.get('childrenDataSet')) {
      setAppChildren(record);
    }
  }, []);
  var handleVersionExpand = useCallback(function (eachRecord) {
    if (!eachRecord.get('expand')) {
      setVersionChildren(eachRecord);
    }

    eachRecord.set('expand', !eachRecord.get('expand'));
  }, []);
  var tableDs = useMemo(function () {
    return new _DataSet(MarketTableDataSet({
      intlPrefix: intlPrefix,
      formatMessage: formatMessage,
      selectedDs: selectedDs,
      setAppChildren: setAppChildren,
      setVersionChildren: setVersionChildren,
      handleMarketServiceCheck: handleMarketServiceCheck,
      handleAppExpand: handleAppExpand,
      handleVersionExpand: handleVersionExpand
    }));
  }, []);
  var allAppDs = useMemo(function () {
    return new _DataSet(AllMarketDataSet());
  }, []);
  var allAppVersionDs = useMemo(function () {
    return new _DataSet(AllMarketVersionDataSet());
  }, []);
  var searchDs = useMemo(function () {
    return new _DataSet(SearchDataSet({
      intlPrefix: intlPrefix,
      formatMessage: formatMessage,
      allAppDs: allAppDs,
      allAppVersionDs: allAppVersionDs,
      tableDs: tableDs
    }));
  }, []);

  var value = _objectSpread(_objectSpread({}, props), {}, {
    formatMessage: formatMessage,
    intlPrefix: intlPrefix,
    prefixCls: 'c7ncd-appService-market',
    tableDs: tableDs,
    searchDs: searchDs,
    mainStore: mainStore,
    setAppChildren: setAppChildren,
    setVersionChildren: setVersionChildren,
    getChecked: getChecked,
    getIndeterminate: getIndeterminate,
    getAppIndeterminate: getAppIndeterminate,
    handleMarketServiceCheck: handleMarketServiceCheck,
    handleAppExpand: handleAppExpand,
    handleVersionExpand: handleVersionExpand
  });

  return /*#__PURE__*/React.createElement(Store.Provider, {
    value: value
  }, children);
}));