text: "use strict";
  Object.defineProperty(exports, "__esModule", { value: true });
  exports.AssemblySymmetryControls = void 0;
  var tslib_1 = require("tslib");
  var jsx_runtime_1 = require("react/jsx-runtime");
  /**
   * Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
   *
   * @author Alexander Rose <alexander.rose@weirdbyte.de>
   */
  var base_1 = require("../../../mol-plugin-ui/base");
  var apply_action_1 = require("../../../mol-plugin-ui/state/apply-action");
  var behavior_1 = require("./behavior");
  var prop_1 = require("./prop");
  var parameters_1 = require("../../../mol-plugin-ui/controls/parameters");
  var param_definition_1 = require("../../../mol-util/param-definition");
  var hierarchy_1 = require("../../../mol-plugin-state/manager/structure/hierarchy");
  var mol_state_1 = require("../../../mol-state");
  var objects_1 = require("../../../mol-plugin-state/objects");
  var mol_task_1 = require("../../../mol-task");
  var icons_1 = require("../../../mol-plugin-ui/controls/icons");
  var AssemblySymmetryControls = /** @class */ (function (_super) {
      (0, tslib_1.__extends)(AssemblySymmetryControls, _super);
      function AssemblySymmetryControls() {
          var _this = _super !== null && _super.apply(this, arguments) || this;
          _this.paramsOnChange = function (options) {
              _this.updateAssemblySymmetry(options);
          };
          return _this;
      }
      AssemblySymmetryControls.prototype.defaultState = function () {
          return {
              header: 'Assembly Symmetry',
              isCollapsed: false,
              isBusy: false,
              isHidden: true,
              brand: { accent: 'cyan', svg: icons_1.ExtensionSvg }
          };
      };
      AssemblySymmetryControls.prototype.componentDidMount = function () {
          var _this = this;
          this.subscribe(this.plugin.managers.structure.hierarchy.behaviors.selection, function () {
              _this.setState({
                  isHidden: !_this.canEnable(),
                  description: hierarchy_1.StructureHierarchyManager.getSelectedStructuresDescription(_this.plugin)
              });
          });
          this.subscribe(this.plugin.state.events.cell.stateUpdated, function (e) {
              if (e.cell.transform.transformer === behavior_1.AssemblySymmetry3D)
                  _this.forceUpdate();
          });
          this.subscribe(this.plugin.behaviors.state.isBusy, function (v) { return _this.setState({ isBusy: v }); });
      };
      Object.defineProperty(AssemblySymmetryControls.prototype, "pivot", {
          get: function () {
              return this.plugin.managers.structure.hierarchy.selection.structures[0];
          },
          enumerable: false,
          configurable: true
      });
      AssemblySymmetryControls.prototype.canEnable = function () {
          var _a, _b;
          var selection = this.plugin.managers.structure.hierarchy.selection;
          if (selection.structures.length !== 1)
              return false;
          var pivot = this.pivot.cell;
          if (!pivot.obj)
              return false;
          return !!((_b = (_a = behavior_1.InitAssemblySymmetry3D.definition).isApplicable) === null || _b === void 0 ? void 0 : _b.call(_a, pivot.obj, pivot.transform, this.plugin));
      };
      AssemblySymmetryControls.prototype.renderEnable = function () {
          var pivot = this.pivot;
          if (!pivot.cell.parent)
              return null;
          return (0, jsx_runtime_1.jsx)(apply_action_1.ApplyActionControl, { state: pivot.cell.parent, action: EnableAssemblySymmetry3D, initiallyCollapsed: true, nodeRef: pivot.cell.transform.ref, simpleApply: { header: 'Enable', icon: icons_1.CheckSvg } }, void 0);
      };
      AssemblySymmetryControls.prototype.renderNoSymmetries = function () {
          return (0, jsx_runtime_1.jsx)("div", (0, tslib_1.__assign)({ className: 'msp-row-text' }, { children: (0, jsx_runtime_1.jsx)("div", { children: "No Symmetries for Assembly" }, void 0) }), void 0);
      };
      Object.defineProperty(AssemblySymmetryControls.prototype, "params", {
          get: function () {
              var _a;
              var structure = (_a = this.pivot.cell.obj) === null || _a === void 0 ? void 0 : _a.data;
              var params = param_definition_1.ParamDefinition.clone(structure ? prop_1.AssemblySymmetryProvider.getParams(structure) : prop_1.AssemblySymmetryProvider.defaultParams);
              params.serverUrl.isHidden = true;
              return params;
          },
          enumerable: false,
          configurable: true
      });
      Object.defineProperty(AssemblySymmetryControls.prototype, "values", {
          get: function () {
              var _a;
              var structure = (_a = this.pivot.cell.obj) === null || _a === void 0 ? void 0 : _a.data;
              if (structure) {
                  return prop_1.AssemblySymmetryProvider.props(structure);
              }
              else {
                  return (0, tslib_1.__assign)((0, tslib_1.__assign)({}, param_definition_1.ParamDefinition.getDefaultValues(prop_1.AssemblySymmetryProvider.defaultParams)), { symmetryIndex: -1 });
              }
          },
          enumerable: false,
          configurable: true
      });
      AssemblySymmetryControls.prototype.updateAssemblySymmetry = function (values) {
          var _a, _b, _c, _d;
          return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
              var s, currValues, b, pd, params, _i, _e, components, name_1;
              return (0, tslib_1.__generator)(this, function (_f) {
                  switch (_f.label) {
                      case 0:
                          s = this.pivot;
                          currValues = prop_1.AssemblySymmetryProvider.props(s.cell.obj.data);
                          if (param_definition_1.ParamDefinition.areEqual(prop_1.AssemblySymmetryProvider.defaultParams, currValues, values))
                              return [2 /*return*/];
                          if (!s.properties) return [3 /*break*/, 2];
                          b = this.plugin.state.data.build();
                          b.to(s.properties.cell).update(function (old) {
                              old.properties[prop_1.AssemblySymmetryProvider.descriptor.name] = values;
                          });
                          return [4 /*yield*/, b.commit()];
                      case 1:
                          _f.sent();
                          return [3 /*break*/, 4];
                      case 2:
                          pd = this.plugin.customStructureProperties.getParams((_a = s.cell.obj) === null || _a === void 0 ? void 0 : _a.data);
                          params = param_definition_1.ParamDefinition.getDefaultValues(pd);
                          params.properties[prop_1.AssemblySymmetryProvider.descriptor.name] = values;
                          return [4 /*yield*/, this.plugin.builders.structure.insertStructureProperties(s.cell, params)];
                      case 3:
                          _f.sent();
                          _f.label = 4;
                      case 4:
                          _i = 0, _e = this.plugin.managers.structure.hierarchy.currentComponentGroups;
                          _f.label = 5;
                      case 5:
                          if (!(_i < _e.length)) return [3 /*break*/, 11];
                          components = _e[_i];
                          if (!(values.symmetryIndex === -1)) return [3 /*break*/, 8];
                          name_1 = (_d = (_c = (_b = components[0]) === null || _b === void 0 ? void 0 : _b.representations[0]) === null || _c === void 0 ? void 0 : _c.cell.transform.params) === null || _d === void 0 ? void 0 : _d.colorTheme.name;
                          if (!(name_1 === prop_1.AssemblySymmetry.Tag.Cluster)) return [3 /*break*/, 7];
                          return [4 /*yield*/, this.plugin.managers.structure.component.updateRepresentationsTheme(components, { color: 'default' })];
                      case 6:
                          _f.sent();
                          _f.label = 7;
                      case 7: return [3 /*break*/, 10];
                      case 8:
                          (0, behavior_1.tryCreateAssemblySymmetry)(this.plugin, s.cell);
                          return [4 /*yield*/, this.plugin.managers.structure.component.updateRepresentationsTheme(components, { color: prop_1.AssemblySymmetry.Tag.Cluster })];
                      case 9:
                          _f.sent();
                          _f.label = 10;
                      case 10:
                          _i++;
                          return [3 /*break*/, 5];
                      case 11: return [2 /*return*/];
                  }
              });
          });
      };
      Object.defineProperty(AssemblySymmetryControls.prototype, "hasAssemblySymmetry3D", {
          get: function () {
              return !this.pivot.cell.parent || !!mol_state_1.StateSelection.findTagInSubtree(this.pivot.cell.parent.tree, this.pivot.cell.transform.ref, prop_1.AssemblySymmetry.Tag.Representation);
          },
          enumerable: false,
          configurable: true
      });
      Object.defineProperty(AssemblySymmetryControls.prototype, "enable", {
          get: function () {
              return !this.hasAssemblySymmetry3D && this.values.symmetryIndex !== -1;
          },
          enumerable: false,
          configurable: true
      });
      Object.defineProperty(AssemblySymmetryControls.prototype, "noSymmetries", {
          get: function () {
              var _a;
              var structure = (_a = this.pivot.cell.obj) === null || _a === void 0 ? void 0 : _a.data;
              var data = structure && prop_1.AssemblySymmetryDataProvider.get(structure).value;
              return data && data.filter(function (sym) { return sym.symbol !== 'C1'; }).length === 0;
          },
          enumerable: false,
          configurable: true
      });
      AssemblySymmetryControls.prototype.renderParams = function () {
          return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: (0, jsx_runtime_1.jsx)(parameters_1.ParameterControls, { params: this.params, values: this.values, onChangeValues: this.paramsOnChange }, void 0) }, void 0);
      };
      AssemblySymmetryControls.prototype.renderControls = function () {
          if (!this.pivot)
              return null;
          if (this.noSymmetries)
              return this.renderNoSymmetries();
          if (this.enable)
              return this.renderEnable();
          return this.renderParams();
      };
      return AssemblySymmetryControls;
  }(base_1.CollapsableControls));
  exports.AssemblySymmetryControls = AssemblySymmetryControls;
  var EnableAssemblySymmetry3D = mol_state_1.StateAction.build({
      from: objects_1.PluginStateObject.Molecule.Structure,
  })(function (_a, plugin) {
      var a = _a.a, ref = _a.ref, state = _a.state;
      return mol_task_1.Task.create('Enable Assembly Symmetry', function (ctx) { return (0, tslib_1.__awaiter)(void 0, void 0, void 0, function () {
          return (0, tslib_1.__generator)(this, function (_a) {
              switch (_a.label) {
                  case 0: return [4 /*yield*/, behavior_1.AssemblySymmetryPreset.apply(ref, Object.create(null), plugin)];
                  case 1:
                      _a.sent();
                      return [2 /*return*/];
              }
          });
      }); });
  });
  //# sourceMappingURL=ui.js.map