import { h } from './chunks/d4789960.js';
import './chunks/456564be.js';
import { u as useProp } from './chunks/c26036df.js';
import './chunks/4ccc6eeb.js';
import { g as getComponentOrDefine } from './chunks/071ed725.js';
import './chunks/23e44913.js';
import './chunks/d91e4e4a.js';
import { O as Option } from './chunks/a02c5734.js';
import './chunks/d76d6172.js';
import './kun-icon.js';
import { e as execWithSixLoad } from './chunks/cf049716.js';
import { r as rn } from './chunks/f484978b.js';
import { u } from './chunks/18a657cf.js';

var n=function(){return (n=Object.assign||function(n){for(var t,e=1,r=arguments.length;e<r;e++)for(var o in t=arguments[e])Object.prototype.hasOwnProperty.call(t,o)&&(n[o]=t[o]);return n}).apply(this,arguments)};function t(n,t,e,r){return new(e||(e=Promise))(function(o,a){function i(n){try{c(r.next(n));}catch(n){a(n);}}function l(n){try{c(r.throw(n));}catch(n){a(n);}}function c(n){var t;n.done?o(n.value):(t=n.value,t instanceof e?t:new e(function(n){n(t);})).then(i,l);}c((r=r.apply(n,t||[])).next());})}function e(n,t){var e,r,o,a,i={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return a={next:l(0),throw:l(1),return:l(2)},"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function l(a){return function(l){return function(a){if(e)throw new TypeError("Generator is already executing.");for(;i;)try{if(e=1,r&&(o=2&a[0]?r.return:a[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,a[1])).done)return o;switch(r=0,o&&(a=[2&a[0],o.value]),a[0]){case 0:case 1:o=a;break;case 4:return i.label++,{value:a[1],done:!1};case 5:i.label++,r=a[1],a=[0];continue;case 7:a=i.ops.pop(),i.trys.pop();continue;default:if(!(o=(o=i.trys).length>0&&o[o.length-1])&&(6===a[0]||2===a[0])){i=0;continue}if(3===a[0]&&(!o||a[1]>o[0]&&a[1]<o[3])){i.label=a[1];break}if(6===a[0]&&i.label<o[1]){i.label=o[1],o=a;break}if(o&&i.label<o[2]){i.label=o[2],i.ops.push(a);break}o[2]&&i.ops.pop(),i.trys.pop();continue}a=t.call(n,i);}catch(n){a=[6,n],r=0;}finally{e=o=0;}if(5&a[0])throw a[1];return {value:a[0]?a[1]:void 0,done:!0}}([a,l])}}}var r=window.location.origin,o=""+(r.indexOf("localhost")>-1?"https://www.xunlianying.vip":r);function a(t,e,r){var a={authorization:""+localStorage.getItem("AUTH")},i={authorization:""+localStorage.getItem("AUTH"),"content-type":"application/json"},l={method:"POST",body:!0!==(null==r?void 0:r.isFormData)?JSON.stringify(e):e,headers:!0!==(null==r?void 0:r.isFormData)?n(n({},a),i):a};return fetch(""+o+t,l).then(function(n){return n.json()}).catch(function(n){return n}).then(function(n){var t=n;if("4001"===String(t.code))null!=(null==r?void 0:r.callback)?r.callback():window.location.replace("/web/plume/login");else if("2001"!==t.code)throw new Error(t.msg);return n})}var i=function(n){return t(void 0,void 0,void 0,function(){return e(this,function(t){return [2,a("/horse/v1/card/learn/addPractice",n)]})})};

var style = ":host {\n}\n";

function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
const KunExersiceSelect = () => {
  var _item$practice;

  const [itemProps, setItem] = useProp("item");
  const [indexEx, setIndex] = useProp("exerciseIndex");
  const item = Option.of(itemProps).getOrElse({
    title: "请选择这个故事",
    options: ["A", "B", "C"],
    practice: {
      answers: ["1", "2"]
    }
  });
  const selectdOptions = _nullishCoalesce(((_item$practice = item.practice) === null || _item$practice === void 0 ? void 0 : _item$practice.answers), () => ( []));

  const selectOption = async index => {
    var _item1$practice;

    let item1;

    if (selectdOptions.indexOf(index) === -1) {
      item1 = rn(item, draft => {
        var _item$practice2;

        draft.practice = {
          practiceId: (_item$practice2 = item.practice) === null || _item$practice2 === void 0 ? void 0 : _item$practice2.practiceId,
          answers: selectdOptions.concat(index)
        };
      });
    } else {
      const indexFound = selectdOptions.findIndex(indexNumber => item === indexNumber);
      item1 = rn(item, draft => {
        var _item$practice3;

        draft.practice = {
          practiceId: (_item$practice3 = item.practice) === null || _item$practice3 === void 0 ? void 0 : _item$practice3.practiceId,
          answers: selectdOptions.slice(indexFound, 1)
        };
      });
    }

    console.log("item", (_item1$practice = item1.practice) === null || _item1$practice === void 0 ? void 0 : _item1$practice.practiceId);
    await execWithSixLoad(async () => {
      var _item1$practice2, _item$practice4;

      const data = {
        seriesId: item.seriesId,
        postId: item.postId,
        exerciseId: item.id,
        practiceId: (_item1$practice2 = item1.practice) === null || _item1$practice2 === void 0 ? void 0 : _item1$practice2.practiceId,
        answer: "",
        answers: item1.practice.answers.map(item2 => String(item2))
      };

      if (((_item$practice4 = item.practice) === null || _item$practice4 === void 0 ? void 0 : _item$practice4.practiceId) == null) {
        await i(data);
      } else {
        await u(data);
      }

      setItem(item1);
    });
  };

  return h("host", null, h("style", null, style), h("div", {
    class: "flex flex-col"
  }, h("div", {
    class: "text-black opacity-75 py-4"
  }, "\u7EC3\u4E60", indexEx, ":", item.title), h("div", null, item.options.map((itemOption, index) => h("div", {
    class: "flex w-full flex-row"
  }, h("div", {
    onclick: () => selectOption(String(index)),
    class: `${selectdOptions.indexOf(String(index)) > -1 ? "bg-orange-400" : ""} justify-between bg-gray-400 w-full flex py-2 px-4 mb-2 cursor-pointer hover:bg-orange-400 hover:text-white`,
    key: String(index)
  }, h("span", null, itemOption), selectdOptions.indexOf(String(index)) > -1 && h("div", {
    class: "flex-none"
  }, h("kun-icon", {
    iconType: "selected",
    iconWidth: 24,
    iconHeight: 24
  }))))))));
};

KunExersiceSelect.props = {
  exerciseIndex: {
    type: Number,
    reflect: true
  },
  item: {
    type: Object,
    reflect: true
  }
};
var kunExersiceSelect = getComponentOrDefine("kun-exersice-select", KunExersiceSelect);

export default kunExersiceSelect;
//# sourceMappingURL=kun-exersice-select.js.map
